//***********************************//
//****      SIMULATION PART    ******//
//***********************************//


// THIS FILE DO THE SIMULATION OF THE MODEL. THERE ARE SEVERAL SUB-ROUTINE TO DO MORE STATISTICS //

// SIMUL10 == perform a simulation for 10 years of newly entrepreneurs entering //
// DECOMP  == decompose the effect of the policies on Uexit and Ax //

// COMMENT ON DISTXXtoYY : for unemployed worker, I assume that we don't care for unemployed with insurance distinction, in particular, I select group with insurance for the statistics above, and I exclude unemployed just losing their rights (and therefore who do not participate in the programs.


/** GET RESIDUALS OF THE HISTOGRAMS, WITH  COSTS **/

// GET RESIDUALS OF THE HISTOGRAM //
void getresid(double x, double *residout, int *ixgrid){
    double xgrid = invexpspace(x,Gridmin,Gridmax,Echelle1,maxgrid);
    *ixgrid=min((maxgrid-1),(int)(floor(xgrid)));
    *ixgrid=max(0,*ixgrid);
    
    if (*ixgrid>=(maxgrid-1)){
        *ixgrid=min((maxgrid-1),(int)(floor(xgrid)));
        *ixgrid=max(0,*ixgrid);
    }
    
    if (*ixgrid<=0){
        *ixgrid=min((maxgrid-1),(int)(floor(xgrid)));
        *ixgrid=max(0,*ixgrid);
    }
    *residout=(x-grid[*ixgrid])/(grid[(*ixgrid+1)]-grid[*ixgrid]);
}


// WITH COST //
void getresidwcost(double amylevel, int *ixgrid, double cost){
    assert(ixgrid);
    double amywcost = max(0.000000001,amylevel - cost);
    *ixgrid=min((maxgrid-1),(int)(floor(invexpspace(amywcost,Gridmin,Gridmax,Echelle1,maxgrid))));
    *ixgrid=max(0,*ixgrid);
}




// IndexPM is used to compute projected Method (when projected method is enabled, then indexPM == 1)

#if indexPM == 1  // indexPM just avoid
    #if NOPOLICY == 1
    void SIMULATION(double *valueUiNE, double *C_UiNE, double *saveUiNE, double *searchUiNE_W, double *searchUiNE_J,
                    double *valueUiE,  double *C_UiE,  double *saveUiE, double *searchUiE_W, double *searchUiE_J,
                    double *valueUnNE, double *C_UnNE, double *saveUnNE, double *searchUnNE_W, double *searchUnNE_J,
                    double *valueUnE,  double *C_UnE,  double *saveUnE, double *searchUnE_W, double *searchUnE_J,
                    double *valueWWNE, double *C_WWNE, double *saveWWNE, double *searchWWNE_J,
                    double *valueWWE,  double *C_WWE,  double *saveWWE, double *searchWWE_J,
                    double *valueJE,   double *C_JE ,  double *saveJE, double *searchJE_W, double *collateralJE,
                    double *valueJNE,  double *C_JNE,  double *saveJNE, double *searchJNE_W, double *collateralJNE, double *sloanJNE, double *rateJNE, double *defaultJNE,
                    double *totalassets, double *totallabor, double *optimaltax, double *generatedmoment,
                    double *start_distJE, double *start_distJNE, double *start_distWWNE, double *start_distWWE, double *start_distUnE, double *start_distUnNE, double *start_distUiNE, double *start_distUiE,
                    double *distJE, double *distJNE, double *distWWNE, double *distWWE, double *distUnE, double *distUnNE, double *distUiNE, double *distUiE,
                    double *fracJJ, double *fracUU, double *meanwagelike, double *optimaltaxSEA, double *defaultratetot, double *prodtotal, double *probleaveU, double *newENTq)
    {
    #endif

    #if SEA == 1
    void SIMULATION(double *valueUiNE, double *C_UiNE, double *saveUiNE, double *searchUiNE_W, double *searchUiNE_J,
                    double *valueUiE,  double *C_UiE,  double *saveUiE, double *searchUiE_W, double *searchUiE_J,
                    double *valueUnNE, double *C_UnNE, double *saveUnNE, double *searchUnNE_W, double *searchUnNE_J,
                    double *valueUnE,  double *C_UnE,  double *saveUnE, double *searchUnE_W, double *searchUnE_J,
                    double *valueWWNE, double *C_WWNE, double *saveWWNE, double *searchWWNE_J,
                    double *valueWWE,  double *C_WWE,  double *saveWWE, double *searchWWE_J,
                    double *valueJE,   double *C_JE,   double *saveJE, double *searchJE_W, double *collateralJE,
                    double *valueJNE,  double *C_JNE,  double *saveJNE, double *searchJNE_W, double *collateralJNE, double *sloanJNE, double *rateJNE, double *defaultJNE,
                    double *valueJEi,  double *C_JEi,  double *saveJEi, double *searchJEi_W, double *collateralJEi,
                    double *valueJNEi, double *C_JNEi, double *saveJNEi, double *searchJNEi_W, double *collateralJNEi, double *sloanJNEi, double *rateJNEi, double *defaultJNEi,
                    double *totalassets, double *totallabor, double *optimaltax, double *optimaltaxSEA, double *generatedmoment,
                    double *start_distJE, double *start_distJNE, double *start_distJEi, double *start_distJNEi, double *start_distWWNE, double *start_distWWE, double *start_distUnE, double *start_distUnNE, double *start_distUiNE, double *start_distUiE,
                    double *distJE, double *distJNE, double *distJEi, double *distJNEi,  double *distWWNE, double *distWWE, double *distUnE, double *distUnNE, double *distUiNE, double *distUiE,
                    double *fracJJ, double *fracUU, double *meanwagelike, double *defaultratetot, double *prodtotal, double *probleaveU, double *newENTq)
    {
    #endif
#endif


#if indexPM == 0
    #if NOPOLICY == 1
    void SIMULATION(double *valueUiNE, double *C_UiNE, double *saveUiNE, double *searchUiNE_W,   double *searchUiNE_J,
                    double *valueUiE,  double *C_UiE,  double *saveUiE,  double *searchUiE_W,    double *searchUiE_J,
                    double *valueUnNE, double *C_UnNE, double *saveUnNE, double *searchUnNE_W,   double *searchUnNE_J,
                    double *valueUnE,  double *C_UnE,  double *saveUnE,  double *searchUnE_W,    double *searchUnE_J,
                    double *valueWWNE, double *C_WWNE, double *saveWWNE, double *searchWWNE_J,
                    double *valueWWE,  double *C_WWE,  double *saveWWE,  double *searchWWE_J,
                    double *valueJE,   double *C_JE,   double *saveJE,   double *searchJE_W,     double *collateralJE,
                    double *valueJNE,  double *C_JNE,  double *saveJNE,  double *searchJNE_W,    double *collateralJNE, double *sloanJNE, double *rateJNE, double *defaultJNE,
                    double *totalassets, double *totallabor, double *optimaltax, double *generatedmoment,
                    double *distJE, double *distJNE, double *distWWNE, double *distWWE, double *distUnE, double *distUnNE, double *distUiNE, double *distUiE)
    {
    #endif
    #if SEA == 1
    void SIMULATION(double *valueUiNE,   double *C_UiNE, double *saveUiNE,   double *searchUiNE_W,   double *searchUiNE_J,
                    double *valueUiE,    double *C_UiE,  double *saveUiE,    double *searchUiE_W,    double *searchUiE_J,
                    double *valueUnNE,   double *C_UnNE, double *saveUnNE,   double *searchUnNE_W,   double *searchUnNE_J,
                    double *valueUnE,    double *C_UnE,  double *saveUnE,    double *searchUnE_W,    double *searchUnE_J,
                    double *valueWWNE,   double *C_WWNE, double *saveWWNE,   double *searchWWNE_J,
                    double *valueWWE,    double *C_WWE,  double *saveWWE,    double *searchWWE_J,
                    double *valueJE,     double *C_JE,   double *saveJE,     double *searchJE_W,     double *collateralJE,
                    double *valueJNE,    double *C_JNE,  double *saveJNE,    double *searchJNE_W,    double *collateralJNE, double *sloanJNE, double *rateJNE, double *defaultJNE,
                    double *valueJEi,    double *C_JEi,  double *saveJEi,    double *searchJEi_W, double *collateralJEi,
                    double *valueJNEi,   double *C_JNEi, double *saveJNEi,   double *searchJNEi_W, double *collateralJNEi, double *sloanJNEi, double *rateJNEi, double *defaultJNEi,
                    double *totalassets, double *totallabor, double *optimaltax,double *optimaltaxSEA, double *generatedmoment,
                    double *distJE, double *distJNE, double *distWWNE, double *distWWE, double *distUnE, double *distUnNE, double *distUiNE, double *distUiE, double *distJEi, double *distJNEi)
    {
    #endif
#endif



/** PREPARE POINTERS FOR BIG LOOP **/
double critDist, distval, distvalold, verifdist, expprob;
int tgrid, igrid, zgrid, ygrid, k, y, e, z, itr;

FILE *tempfileoutfile;

/*****************************************************************************************/
///////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////// INITIALIZATION HERE ///////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
/*****************************************************************************************/


// ENTREPRENEUR
double *distJEold, *distassetJE, *saveresJE, fracJE;
double *distJNEold, *distassetJNE, *saveresJNE, fracJNE;
double tempWWNE_JNE_0, tempWWNE_JNEi_0, tempUiNE_JNE_0, tempUnNE_JNE_0, tempWWNE_JNE_1, tempWWNE_JNEi_1, tempUiNE_JNE_1, tempUnNE_JNE_1;
double tempWWE_JE_0, tempWWE_JEi_0, tempUiE_JE_0, tempUnE_JE_0, tempWWE_JE_1, tempWWE_JEi_1, tempUiE_JE_1, tempUnE_JE_1;
double tempWWE_JNE_0, tempWWE_JNE_1, tempWWE_JNEi_0, tempWWE_JNEi_1,tempUiE_JNE_0, tempUiE_JNE_1, tempUnE_JNE_0, tempUnE_JNE_1;
double tempWWE_Ui_JE_0, tempWWE_Ui_JE_1, tempWWE_Ui_JNE_0, tempWWE_Ui_JNE_1, tempWWNE_Ui_JNE_0, tempWWNE_Ui_JNE_1;
            // occuaption X with saving gets value Y is written tempX_Y (when next type no more excluded, but saving decision is already made).
double *distJEtoWWE, *distJEtoUiE, *distJEtoUnE, *distJEtoUiNE, *distJEtoUnNE, *distJEtoWWNE, *distJNEtoWWE, *distJNEtoUiE, *distJNEtoUnE, *distJNEtoUiNE, *distJNEtoUnNE, *distJNEtoWWNE, *distWWEfromUiEtoJNE, *distWWEfromUiEtoJE, *distWWNEfromUiNEtoJNE; // for flows
double *distUiEtoJE_select, *distUiEtoJNE_select, *distUiNEtoJNE_select;
int *isaveJE, *isaveJNE;

#if SEA == 1
    double *distJEiold, *distassetJEi, *saveresJEi, fracJEi;
    double *distJNEiold, *distassetJNEi, *saveresJNEi, fracJNEi;
    double tempUiE_JEi_0, tempUiE_JEi_1, tempUiE_JNEi_0, tempUiE_JNEi_1, tempUiNE_JNEi_0, tempUiNE_JNEi_1;
    int *isaveJEi, *isaveJNEi;
#endif

double tempUiE_JE_0_select, tempUiE_JE_1_select, tempUiE_JNE_0_select, tempUiE_JNE_1_select, tempUiNE_JNE_0_select, tempUiNE_JNE_1_select;


#if SEA == 1
// VECTOR OF MUENDO FOR DRI //
double *MUENDOvecB, *MUENDOvecR, *MUENDOvecEhat;

MUENDOvecR      = (double *) calloc((ifulldimEE), sizeof(double));
MUENDOvecB      = (double *) calloc((ifulldimEE), sizeof(double));
MUENDOvecEhat   = (double *) calloc((ifulldimEE), sizeof(double));

for(igrid=0;igrid<maxgrid;igrid++){
    for(tgrid = 0.0; tgrid < maxtheta; tgrid ++){
        for(zgrid = 0.0; zgrid < maxfirmtype; zgrid++){
            for(e = 0; e < maxfirmtype; e++){
                
                MUENDOvecR[inxEE(igrid,tgrid,zgrid,e)] = muendoSEA(profitRfun(igrid,e,tgrid,collateralJNEi[inxE(igrid,tgrid,zgrid)],rateJNEi[inxE(igrid,tgrid,zgrid)]),tgrid);
                
                MUENDOvecB[inxEE(igrid,tgrid,zgrid,e)] = muendoSEA(0.0,tgrid);
                
                MUENDOvecEhat[inxEE(igrid,tgrid,zgrid,e)] = muendoSEA(profitEhatfun(igrid,e,tgrid,collateralJEi[inxE(igrid,tgrid,zgrid)]),tgrid);
                
                if(MUENDOvecEhat[inxEE(igrid,tgrid,zgrid,e)] < 0 || MUENDOvecEhat[inxEE(igrid,tgrid,zgrid,e)] > mupar + 0.00000001){printf("mistake MUENDOvecEhat %f ", mupar*(bstarcostfun(profitEhatfun(igrid,zgrid,tgrid,collateralJEi[inxE(igrid,tgrid,zgrid)]),tgrid))/((1.0-taxrate)*rhostar*wstar*prod[tgrid]));getchar();}
                if(MUENDOvecB[inxEE(igrid,tgrid,zgrid,e)] < 0 || MUENDOvecB[inxEE(igrid,tgrid,zgrid,e)] > mupar + 0.00000001 ){printf("mistake MUENDOvecB %f", mupar);getchar();}
                if(MUENDOvecR[inxEE(igrid,tgrid,zgrid,e)] < 0 || MUENDOvecR[inxEE(igrid,tgrid,zgrid,e)] > mupar + 0.00000001){printf("mistake MUENDOvecR %f", mupar*(bstarcostfun(profitRfun(igrid,zgrid,tgrid,collateralJNEi[inxE(igrid,tgrid,zgrid)],rateJNEi[inxE(igrid,tgrid,zgrid)]),tgrid))/((1.0-taxrate)*rhostar*wstar*prod[tgrid]));getchar();}
            }
            
        }
    }
}

#endif




// WORKER
double *distWWEold, *saveresWWE, fracWWE, *distWWNEold, *saveresWWNE, fracWWNE, *distWWEtoJE, *distWWEtoJNE, *distWWEtoUiE, *distWWEtoUiNE, *distWWNEtoUiNE, *distWWNEtoJNE, *necessityWWE, *necessityWWNE;
int *isaveWWE, *isaveWWNE;


// ST UNEMPLOYED
double *distUiEold, *saveresUiE, *distUnEold, *saveresUnE, *distUiNEold, *saveresUiNE, *distUnNEold, *saveresUnNE, *distUiEtoJE, *distUiEtoJNE, *distUiEtoWWE, *distUiEtoWWNE, *distUiEtoUnE, *distUiEtoUnNE, *distUiEtoUiNE, *distUnEtoJE, *distUnEtoJNE, *distUnEtoWWE, *distUnEtoWWNE, *distUnEtoUnNE, *distUiNEtoJNE, *distUiNEtoWWNE, *distUiNEtoUnNE, *distUnNEtoJNE, *distUnNEtoWWNE, *necessityUiE, *necessityUiNE, *necessityUnE, *necessityUnNE;
int *isaveUiE, *isaveUiNE, *isaveUnE, *isaveUnNE;


// INITIALIZE DISTRIBUTION //
distUnEold  = (double *) calloc((ifulldim), sizeof(double));
distUiEold  = (double *) calloc((ifulldim), sizeof(double));
distUnNEold = (double *) calloc((ifulldim), sizeof(double));
distUiNEold = (double *) calloc((ifulldim), sizeof(double));
distJEold   = (double *) calloc((ifulldimE), sizeof(double));
distJNEold  = (double *) calloc((ifulldimE), sizeof(double));

#if SEA == 1
    distJEiold = (double *) calloc((ifulldimE), sizeof(double));
    distJNEiold = (double *) calloc((ifulldimE), sizeof(double));
#endif

distWWEold  = (double *) calloc((ifulldimW), sizeof(double));
distWWNEold = (double *) calloc((ifulldimW), sizeof(double));


// INITIALIZE SAVINGS //
saveresUiE  = (double *) calloc((ifulldim), sizeof(double));
saveresUnE  = (double *) calloc((ifulldim), sizeof(double));
saveresUiNE = (double *) calloc((ifulldim), sizeof(double));
saveresUnNE = (double *) calloc((ifulldim), sizeof(double));
saveresWWE  = (double *) calloc((ifulldimW), sizeof(double));
saveresWWNE = (double *) calloc((ifulldimW), sizeof(double));
saveresJE   = (double *) calloc((ifulldimEE), sizeof(double));
saveresJNE  = (double *) calloc((ifulldimEE), sizeof(double));
isaveUiE    = (int *) calloc((ifulldim), sizeof(int));
isaveUnE    = (int *) calloc((ifulldim), sizeof(int));
isaveUiNE   = (int *) calloc((ifulldim), sizeof(int));
isaveUnNE   = (int *) calloc((ifulldim), sizeof(int));
isaveWWE    = (int *) calloc((ifulldimW), sizeof(int));
isaveWWNE   = (int *) calloc((ifulldimW), sizeof(int));
isaveJE     = (int *) calloc((ifulldimEE), sizeof(int));
isaveJNE    = (int *) calloc((ifulldimEE), sizeof(int));


#if SEA == 1
    saveresJEi  = (double *) calloc((ifulldimEE), sizeof(double));
    saveresJNEi = (double *) calloc((ifulldimEE), sizeof(double));
    isaveJEi    = (int *) calloc((ifulldimEE), sizeof(int));
    isaveJNEi   = (int *) calloc((ifulldimEE), sizeof(int));
#endif



// INITIALIZE FLOWS + NECESSITY SHARE //
distUiEtoJE     = (double *) calloc((ifulldim), sizeof(double));
distUiEtoJNE    = (double *) calloc((ifulldim), sizeof(double));
distUiEtoWWE    = (double *) calloc((ifulldim), sizeof(double));
distUiEtoWWNE   = (double *) calloc((ifulldim), sizeof(double));
distUiEtoUnE    = (double *) calloc((ifulldim), sizeof(double));
distUiEtoUnNE   = (double *) calloc((ifulldim), sizeof(double));
distUiEtoUiNE   = (double *) calloc((ifulldim), sizeof(double));
distUnEtoJE     = (double *) calloc((ifulldim), sizeof(double));
distUnEtoJNE    = (double *) calloc((ifulldim), sizeof(double));
distUnEtoWWE    = (double *) calloc((ifulldim), sizeof(double));
distUnEtoWWNE   = (double *) calloc((ifulldim), sizeof(double));
distUnEtoUnNE   = (double *) calloc((ifulldim), sizeof(double));
distUiNEtoJNE   = (double *) calloc((ifulldim), sizeof(double));
distUiNEtoWWNE  = (double *) calloc((ifulldim), sizeof(double));
distUiNEtoUnNE  = (double *) calloc((ifulldim), sizeof(double));
distUnNEtoJNE   = (double *) calloc((ifulldim), sizeof(double));
distUnNEtoWWNE  = (double *) calloc((ifulldim), sizeof(double));
distUiNEtoJNE_select    = (double *) calloc((ifulldim), sizeof(double));
distUiEtoJE_select      = (double *) calloc((ifulldim), sizeof(double));
distUiEtoJNE_select     = (double *) calloc((ifulldim), sizeof(double));
necessityUiE    = (double *) calloc((ifulldim), sizeof(double));
necessityUiNE   = (double *) calloc((ifulldim), sizeof(double));
necessityUnE    = (double *) calloc((ifulldim), sizeof(double));
necessityUnNE   = (double *) calloc((ifulldim), sizeof(double));

distWWEtoJE     = (double *) calloc((ifulldimW), sizeof(double));
distWWEtoJNE    = (double *) calloc((ifulldimW), sizeof(double));
distWWEtoUiE    = (double *) calloc((ifulldimW), sizeof(double));
distWWEtoUiNE   = (double *) calloc((ifulldimW), sizeof(double));
distWWNEtoUiNE  = (double *) calloc((ifulldimW), sizeof(double));
distWWNEtoJNE   = (double *) calloc((ifulldimW), sizeof(double));
necessityWWE    = (double *) calloc((ifulldimW), sizeof(double));
necessityWWNE   = (double *) calloc((ifulldimW), sizeof(double));

distJEtoWWE     = (double *) calloc((ifulldimE), sizeof(double));
distJEtoUiE     = (double *) calloc((ifulldimE), sizeof(double));
distJEtoUnE     = (double *) calloc((ifulldimE), sizeof(double));
distJEtoUiNE    = (double *) calloc((ifulldimE), sizeof(double));
distJEtoUnNE    = (double *) calloc((ifulldimE), sizeof(double));
distJEtoWWNE    = (double *) calloc((ifulldimE), sizeof(double));
distJNEtoWWE    = (double *) calloc((ifulldimE), sizeof(double));
distJNEtoUiE    = (double *) calloc((ifulldimE), sizeof(double));
distJNEtoUnE    = (double *) calloc((ifulldimE), sizeof(double));
distJNEtoUiNE   = (double *) calloc((ifulldimE), sizeof(double));
distJNEtoUnNE   = (double *) calloc((ifulldimE), sizeof(double));
distJNEtoWWNE   = (double *) calloc((ifulldimE), sizeof(double));
distWWEfromUiEtoJE      = (double *) calloc((ifulldim), sizeof(double));
distWWEfromUiEtoJNE     = (double *) calloc((ifulldim), sizeof(double));
distWWNEfromUiNEtoJNE   = (double *) calloc((ifulldim), sizeof(double));


////////////////
// STATISTICS //
////////////////

/* distribution */
double *distWWearning, *disttotal, *distasset, *distassetJ, *distassetW, *distassetUi, *distassetUn, *fracJpability, *fracWWpability, *fracUpability, *aggWWtoJ, *aggJtoWW, *aggUtoJ, *aggUtoWW, *aggWWtoU, *aggJtoU, fracUi, fracUn, fracW, fracJ, fracJi, fracD, defaultrate, meanwage, costsubsidy, fracWWsubsidy;
distassetJ      = (double *) calloc((maxgrid), sizeof(double));
distassetW      = (double *) calloc((maxgrid), sizeof(double));
distassetUi     = (double *) calloc((maxgrid), sizeof(double));
distassetUn     = (double *) calloc((maxgrid), sizeof(double));
distasset       = (double *) calloc((maxgrid), sizeof(double));
disttotal       = (double *) calloc((ifulldim), sizeof(double));
distWWearning   = (double *) calloc((ifulldimEARN), sizeof(double));
aggWWtoJ        = (double *) calloc((maxtheta), sizeof(double));
aggJtoWW        = (double *) calloc((maxtheta), sizeof(double));
aggWWtoU        = (double *) calloc((maxtheta), sizeof(double));
aggJtoU         = (double *) calloc((maxtheta), sizeof(double));
aggUtoJ         = (double *) calloc((maxtheta), sizeof(double));
aggUtoWW        = (double *) calloc((maxtheta), sizeof(double));
fracWWpability  = (double *) calloc((maxtheta), sizeof(double));
fracJpability   = (double *) calloc((maxtheta), sizeof(double));
fracUpability   = (double *) calloc((maxtheta), sizeof(double));




/** FOR STATISTICS **/
double top001p, top01p, top05p, top10p, top20p,top40p,bot20p,bot40p,bot60p,bot80p, GINIval, GINIearn, mworthJ,mworthW,mworthUi,mworthUn, mworthall;
/* Aggregate */
double avgfirmsize, capitalJ,laborJ,frac_employer,corporate_labor,productionJ,totalcapital,corproduction,totalproduction, KY,  totalassetsJ, ratioAXA, AXtot, Atot, indicatorAX;
/* Welfare (utilitarian) */
double welfareUi, welfareUn, welfareW, welfareJ, welfaretot, C_welfaretot;
/* Flows */
double fracWWtoJ, fracWWtoUi, fracUitoWW, fracUitoJ, fracJtoU, fracJtoWW, fracUntoWW, fracUntoJ, necessityUi, necessityUn, necessityWW, necessityUiTHETA[maxtheta], necessityUnTHETA[maxtheta], necessityWWTHETA[maxtheta];
// TAXES & DEBT
double expenditure, revenubasis, corpbase, debt, basebeforetaxJNE, basebeforetaxJE, costSEA, taxrateJ, returnsbeforetaxJE, returnsbeforetaxJEi, returnsbeforetaxJNE, returnsbeforetaxJNEi,returnsaftertaxJE,returnsaftertaxJEi,returnsaftertaxJNE,returnsaftertaxJNEi, basebeforetaxJEi, basebeforetaxJNEi;
// ENTREPRENEURS POOL //
double ENT[maxtheta], SUM[maxtheta], AVGFIRM[maxtheta], AVGASSET[maxtheta], ENTfrac[maxtheta], leverage;


/** FILES **/
FILE *distoutJ, *distoutWW, *distoutU, *distoutfirm, *distoutwealth, *distoutfile;


#if indexPM == 1  // indexPM just avoid
    bascule(start_distWWE,distWWE,ifulldimW);
    bascule(start_distWWNE,distWWNE,ifulldimW);

    bascule(start_distUiNE,distUiNE,ifulldim);
    bascule(start_distUnNE,distUnNE,ifulldim);
    bascule(start_distUiE,distUiE,ifulldim);
    bascule(start_distUnE,distUnE,ifulldim);

    bascule(start_distJE,distJE,ifulldimE);
    bascule(start_distJNE,distJNE,ifulldimE);

    #if SEA == 1
            bascule(start_distJEi,distJEi,ifulldimE);
            bascule(start_distJNEi,distJNEi,ifulldimE);
    #endif
#endif
        
        

// GET THE RESIDUALS savings.
for(tgrid = 0.0; tgrid < maxtheta; tgrid++){
    for(igrid=0;igrid<maxgrid;igrid++){
        for(zgrid=0;zgrid<maxfirmtype;zgrid++){
            for(e=0; e<maxfirmtype; e++) {
                getresid(saveJE[inxEE(igrid, tgrid, zgrid, e)],&saveresJE[inxEE(igrid, tgrid, zgrid, e)],&isaveJE[inxEE(igrid, tgrid, zgrid, e)]);
                getresid(saveJNE[inxEE(igrid, tgrid, zgrid, e)],&saveresJNE[inxEE(igrid, tgrid, zgrid, e)],&isaveJNE[inxEE(igrid, tgrid, zgrid, e)]);
                
                #if SEA == 1
                    getresid(saveJEi[inxEE(igrid, tgrid, zgrid, e)],&saveresJEi[inxEE(igrid, tgrid, zgrid, e)],&isaveJEi[inxEE(igrid, tgrid, zgrid, e)]);
                    getresid(saveJNEi[inxEE(igrid, tgrid, zgrid, e)],&saveresJNEi[inxEE(igrid, tgrid, zgrid, e)],&isaveJNEi[inxEE(igrid, tgrid, zgrid, e)]);
                    
                    if(saveresJNEi[inxE(igrid, tgrid, zgrid)] < -0.000000001 || saveresJNEi[inxE(igrid, tgrid, zgrid)] > 1.000000001 || saveresJEi[inxE(igrid, tgrid, zgrid)] < -0.000000001 || saveresJEi[inxE(igrid, tgrid, zgrid)] > 1.000000001) {
                    printf("MISTAKE"); getchar();
                    }
                #endif
            
                if(saveresJNE[inxE(igrid, tgrid, zgrid)] < -0.000000001 || saveresJNE[inxE(igrid, tgrid, zgrid)] > 1.000000001 || saveresJE[inxE(igrid, tgrid, zgrid)] < -0.000000001 || saveresJE[inxE(igrid, tgrid, zgrid)] > 1.000000001) {
                printf("MISTAKE"); getchar();
                }
            }
        } // end zgrid
        
        for(ygrid=0; ygrid<maxyprod; ygrid++) {
            getresid(saveWWE[inxW(igrid, tgrid, ygrid)],&saveresWWE[inxW(igrid, tgrid, ygrid)],&isaveWWE[inxW(igrid, tgrid, ygrid)]);
            getresid(saveWWNE[inxW(igrid, tgrid, ygrid)],&saveresWWNE[inxW(igrid, tgrid, ygrid)],&isaveWWNE[inxW(igrid, tgrid, ygrid)]);
            
            if(saveresWWNE[inxW(igrid, tgrid, ygrid)] < -0.000000001 || saveresWWNE[inxW(igrid, tgrid, ygrid)] > 1.000000001 || saveresWWE[inxW(igrid, tgrid, ygrid)] < -0.000000001 || saveresWWE[inxW(igrid, tgrid, ygrid)] > 1.000000001) {
                printf("MISTAKE"); getchar();
            }
        } // end ygrid
        
        getresid(saveUiE[inx(igrid, tgrid)],&saveresUiE[inx(igrid, tgrid)],&isaveUiE[inx(igrid, tgrid)]);
        getresid(saveUnE[inx(igrid, tgrid)],&saveresUnE[inx(igrid, tgrid)],&isaveUnE[inx(igrid, tgrid)]);
        
        getresid(saveUiNE[inx(igrid, tgrid)],&saveresUiNE[inx(igrid, tgrid)],&isaveUiNE[inx(igrid, tgrid)]);
        getresid(saveUnNE[inx(igrid, tgrid)],&saveresUnNE[inx(igrid, tgrid)],&isaveUnNE[inx(igrid, tgrid)]);
        
        if(saveresUiNE[inx(igrid, tgrid)] < -0.000000001 || saveresUiNE[inx(igrid, tgrid)] > 1.000000001 ) {
            printf("MISTAKE"); getchar();
        }
        
    } // end igrid
} // end tgrid

        

        
        
/*****************************************************************************************/
/////////////////////////////////// START BIG LOOP HERE ///////////////////////////////////
/*****************************************************************************************/

        

critDist    =   1.0;
itr         =   0;

#if indexPM == 0
while((critDist>epsilonDist) && (itr < maxiterDist)){
#endif
    
itr++;

/////////////////////////////////////////
// INITIALIZE ALL DISTRIBUTION TO ZERO //
/////////////////////////////////////////

verifdist = 0.0;
for(tgrid = 0.0; tgrid < maxtheta; tgrid++){
    for(igrid=0;igrid<maxgrid;igrid++){
        for(zgrid=0;zgrid<maxfirmtype;zgrid++){
            
            verifdist += distJE[inxE(igrid, tgrid, zgrid)] + distJNE[inxE(igrid, tgrid, zgrid)];
            distJEold[inxE(igrid, tgrid, zgrid)]        = distJE[inxE(igrid, tgrid, zgrid)];
            distJNEold[inxE(igrid, tgrid, zgrid)]       = distJNE[inxE(igrid, tgrid, zgrid)];
            distJE[inxE(igrid, tgrid, zgrid)]           =    0.0;
            distJNE[inxE(igrid, tgrid, zgrid)]          =    0.0;

            #if SEA == 1
                distJEiold[inxE(igrid, tgrid, zgrid)]       = distJEi[inxE(igrid, tgrid, zgrid)];
                distJNEiold[inxE(igrid, tgrid, zgrid)]      = distJNEi[inxE(igrid, tgrid, zgrid)];
                distJEi[inxE(igrid, tgrid, zgrid)]          =    0.0;
                distJNEi[inxE(igrid, tgrid, zgrid)]         =    0.0;
            #endif
            
            distJEtoWWE[inxE(igrid, tgrid, zgrid)]      =    0.0;
            distJEtoUiE[inxE(igrid, tgrid, zgrid)]      =    0.0;
            distJEtoUnE[inxE(igrid, tgrid, zgrid)]      =    0.0;
            distJEtoUiNE[inxE(igrid, tgrid, zgrid)]     =    0.0;
            distJEtoUnNE[inxE(igrid, tgrid, zgrid)]     =    0.0;
            distJEtoWWNE[inxE(igrid, tgrid, zgrid)]     =    0.0;
            distJNEtoWWE[inxE(igrid, tgrid, zgrid)]     =    0.0;
            distJNEtoUiE[inxE(igrid, tgrid, zgrid)]     =    0.0;
            distJNEtoUnE[inxE(igrid, tgrid, zgrid)]     =    0.0;
            distJNEtoUiNE[inxE(igrid, tgrid, zgrid)]    =    0.0;
            distJNEtoUnNE[inxE(igrid, tgrid, zgrid)]    =    0.0;
            distJNEtoWWNE[inxE(igrid, tgrid, zgrid)]    =    0.0;
        
        }
        
        for(ygrid=0; ygrid < maxyprod; ygrid++) {
            verifdist += distWWE[inxW(igrid, tgrid, ygrid)] + distWWNE[inxW(igrid, tgrid, ygrid)];
            
            distWWEold[inxW(igrid, tgrid, ygrid)]       =    distWWE[inxW(igrid, tgrid, ygrid)];
            distWWNEold[inxW(igrid, tgrid, ygrid)]      =    distWWNE[inxW(igrid, tgrid, ygrid)];
            
            distWWE[inxW(igrid, tgrid, ygrid)]          =    0.0;
            distWWNE[inxW(igrid, tgrid, ygrid)]         =    0.0;
            
            distWWEtoJE[inxW(igrid, tgrid, ygrid)]      =    0.0;
            distWWEtoJNE[inxW(igrid, tgrid, ygrid)]     =    0.0;
            distWWEtoUiE[inxW(igrid, tgrid, ygrid)]     =    0.0;
            distWWEtoUiNE[inxW(igrid, tgrid, ygrid)]    =    0.0;
            distWWNEtoUiNE[inxW(igrid, tgrid, ygrid)]   =    0.0;
            distWWNEtoJNE[inxW(igrid, tgrid, ygrid)]    =    0.0;
            necessityWWE[inxW(igrid, tgrid, ygrid)]     =    0.0;
            necessityWWNE[inxW(igrid, tgrid, ygrid)]    =    0.0;

        }
        
        verifdist += distUiE[inx(igrid, tgrid)];
        verifdist += distUnE[inx(igrid, tgrid)];
        verifdist += distUiNE[inx(igrid, tgrid)];
        verifdist += distUnNE[inx(igrid, tgrid)];

        distUiEold[inx(igrid, tgrid)]      =   distUiE[inx(igrid, tgrid)];
        distUnEold[inx(igrid, tgrid)]      =   distUnE[inx(igrid, tgrid)];
        distUiNEold[inx(igrid, tgrid)]     =   distUiNE[inx(igrid, tgrid)];
        distUnNEold[inx(igrid, tgrid)]     =   distUnNE[inx(igrid, tgrid)];
        
        distUiE[inx(igrid, tgrid)]          =   0.0;
        distUnE[inx(igrid, tgrid)]          =   0.0;
        distUiNE[inx(igrid, tgrid)]         =   0.0;
        distUnNE[inx(igrid, tgrid)]         =   0.0;
        
        distUiEtoJE[inx(igrid, tgrid)]      =   0.0;
        distUiEtoJNE[inx(igrid, tgrid)]     =   0.0;
        distUiEtoWWE[inx(igrid, tgrid)]     =   0.0;
        distUiEtoWWNE[inx(igrid, tgrid)]    =   0.0;
        distUiEtoUnE[inx(igrid, tgrid)]     =   0.0;
        distUiEtoUnNE[inx(igrid, tgrid)]    =   0.0;
        distUnEtoJE[inx(igrid, tgrid)]      =   0.0;
        distUnEtoJNE[inx(igrid, tgrid)]     =   0.0;
        distUnEtoWWE[inx(igrid, tgrid)]     =   0.0;
        distUnEtoWWNE[inx(igrid, tgrid)]    =   0.0;
        distUiNEtoJNE[inx(igrid, tgrid)]    =   0.0;
        distUiNEtoWWNE[inx(igrid, tgrid)]   =   0.0;
        distUiNEtoUnNE[inx(igrid, tgrid)]   =   0.0;
        distUnNEtoJNE[inx(igrid, tgrid)]    =   0.0;
        distUnNEtoWWNE[inx(igrid, tgrid)]   =   0.0;
        distWWEfromUiEtoJE[inx(igrid, tgrid)]       = 0.0;
        distWWEfromUiEtoJNE[inx(igrid, tgrid)]      = 0.0;
        distWWNEfromUiNEtoJNE[inx(igrid, tgrid)]    = 0.0;
        necessityUiE[inx(igrid, tgrid)]     =   0.0;
        necessityUiNE[inx(igrid, tgrid)]    =   0.0;
        necessityUnE[inx(igrid, tgrid)]     =   0.0;
        necessityUnNE[inx(igrid, tgrid)]    =   0.0;
        
    }
}




//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 
    
// VERIFICATION VARIABLES //
distval         =   0.0;
distvalold      =   0.0;
fracWWsubsidy   =   0.0;
    

/* by default, we assume that WW is prefered to Entrepreneur which is prefered to Unemployment if indifferent */
for(tgrid = 0.0; tgrid < maxtheta; tgrid++){
    for(igrid=0;igrid<maxgrid;igrid++){
        for(k = 0; k < maxtheta; k++){
        
  
        /**************/
        /*** WORKER ***/
        /**************/
        
        for(ygrid=0; ygrid < maxyprod; ygrid++) {
        
            // determine the expected value of being an entrepreneur.
            tempWWE_JE_0 = 0.0; tempWWE_JE_1 = 0.0; tempWWE_JNE_0 = 0.0; tempWWE_JNE_1 = 0.0; tempWWNE_JNE_0 = 0.0;tempWWNE_JNE_1 = 0.0;
            tempWWE_JEi_0 = 0.0;tempWWE_JEi_1 = 0.0; tempWWE_JNEi_0 = 0.0;tempWWE_JNEi_1 = 0.0;
            tempWWNE_JNEi_0 = 0.0;tempWWNE_JNEi_1 = 0.0; tempWWE_Ui_JE_0 = 0.0;tempWWE_Ui_JE_1 = 0.0;
            tempWWE_Ui_JNE_0 = 0.0;tempWWE_Ui_JNE_1 = 0.0; tempWWNE_Ui_JNE_0 = 0.0;tempWWNE_Ui_JNE_1 = 0.0;
            
            for(e = 0; e < maxfirmtype; e++) {
                tempWWE_JE_0    += mzinv[e]*valueJE[inxE(isaveWWE[inxW(igrid, tgrid, ygrid)], k, e)];
                tempWWE_JE_1    += mzinv[e]*valueJE[inxE(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k, e)];
                tempWWE_JNE_0   += mzinv[e]*valueJNE[inxE(isaveWWE[inxW(igrid, tgrid, ygrid)], k, e)];
                tempWWE_JNE_1   += mzinv[e]*valueJNE[inxE(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k, e)];
                tempWWNE_JNE_0  += mzinv[e]*valueJNE[inxE(isaveWWNE[inxW(igrid, tgrid, ygrid)], k, e)];
                tempWWNE_JNE_1  += mzinv[e]*valueJNE[inxE(min((maxgrid-1),(isaveWWNE[inxW(igrid, tgrid, ygrid)]+1)), k, e)];
                
                #if SEA == 1
                    tempWWE_JEi_0   += mzinv[e]*valueJEi[inxE(isaveWWE[inxW(igrid, tgrid, ygrid)], k, e)];
                    tempWWE_JEi_1   += mzinv[e]*valueJEi[inxE(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k, e)];
                    tempWWE_JNEi_0  += mzinv[e]*valueJNEi[inxE(isaveWWE[inxW(igrid, tgrid, ygrid)], k, e)];
                    tempWWE_JNEi_1  += mzinv[e]*valueJNEi[inxE(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k, e)];
                    tempWWNE_JNEi_0 += mzinv[e]*valueJNEi[inxE(isaveWWNE[inxW(igrid, tgrid, ygrid)], k, e)];
                    tempWWNE_JNEi_1 += mzinv[e]*valueJNEi[inxE(min((maxgrid-1),(isaveWWNE[inxW(igrid, tgrid, ygrid)]+1)), k, e)];
                #endif
            }
            
        
            for(y = 0; y < maxyprod; y++) {
            
                /*** EXCLUDED ***/
                
                // TRANSITION TO UNEMPLOYED EXCLUDED WITH INSURANCE //
                // not receive phipar, is laid off, does not receive business idea //
                expprob = mprod[tgrid][k]*myprod[ygrid][y]*(1.0 - phipar)*etapar[tgrid]*(1.0 - piE(searchWWE_J[inxW(igrid, tgrid, ygrid)]));

                distUiE[inx(isaveWWE[inxW(igrid, tgrid, ygrid)], k)] += expprob*(1.0 - saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)];
                distUiE[inx(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k)] += expprob*(saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)];
                
                distWWEtoUiE[inxW(igrid, tgrid, ygrid)] += expprob*(1.0 - saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)] + expprob*(saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)];
                
                // not receive phipar, is laid off, does receive business idea //
                #if NOPOLICY == 1
                expprob = mprod[tgrid][k]*myprod[ygrid][y]*(1.0 - phipar)*etapar[tgrid]*(piE(searchWWE_J[inxW(igrid, tgrid, ygrid)]));

                distUiE[inx(isaveWWE[inxW(igrid, tgrid, ygrid)], k)] += expprob*(1.0 - saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JE_0 < (valueUiE[inx(isaveWWE[inxW(igrid, tgrid, ygrid)], k)])))?(1.0):(0.0));
                distUiE[inx(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k)] += expprob*(saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JE_1 < (valueUiE[inx(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k)])))?(1.0):(0.0));
                
                distWWEtoUiE[inxW(igrid, tgrid, ygrid)] += expprob*(1.0 - saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JE_0 < (valueUiE[inx(isaveWWE[inxW(igrid, tgrid, ygrid)], k)])))?(1.0):(0.0)) + expprob*(saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JE_1 < (valueUiE[inx(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k)])))?(1.0):(0.0));
                #endif
                
                #if SEA == 1
                expprob = mprod[tgrid][k]*myprod[ygrid][y]*(1.0 - phipar)*etapar[tgrid]*(piE(searchWWE_J[inxW(igrid, tgrid, ygrid)]));

                distUiE[inx(isaveWWE[inxW(igrid, tgrid, ygrid)], k)] += expprob*(1.0 - saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JEi_0 < (valueUiE[inx(isaveWWE[inxW(igrid, tgrid, ygrid)], k)])))?(1.0):(0.0));
                distUiE[inx(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k)] += expprob*(saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JEi_1 < (valueUiE[inx(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k)])))?(1.0):(0.0));
                
                distWWEtoUiE[inxW(igrid, tgrid, ygrid)] += expprob*(1.0 - saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JEi_0 < (valueUiE[inx(isaveWWE[inxW(igrid, tgrid, ygrid)], k)])))?(1.0):(0.0)) + expprob*(saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JEi_1 < (valueUiE[inx(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k)])))?(1.0):(0.0));
                #endif
                
                
                // TRANSITION TO UNEMPLOYED NON EXCLUDED WITH INSURANCE
                // receive phipar, is laid off, does not receive business idea //
                expprob = mprod[tgrid][k]*myprod[ygrid][y]*(phipar)*etapar[tgrid]*(1.0 - piE(searchWWE_J[inxW(igrid, tgrid, ygrid)]));

                distUiNE[inx(isaveWWE[inxW(igrid, tgrid, ygrid)], k)] += expprob*(1.0 - saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)];
                distUiNE[inx(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k)] += expprob*(saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)];
                
                distWWEtoUiNE[inxW(igrid, tgrid, ygrid)] += expprob*(1.0 - saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)] + expprob*(saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)];
                
                // receive phipar, is laid off, does receive business idea //
                #if NOPOLICY == 1
                expprob = mprod[tgrid][k]*myprod[ygrid][y]*(phipar)*etapar[tgrid]*(piE(searchWWE_J[inxW(igrid, tgrid, ygrid)]));

                distUiNE[inx(isaveWWE[inxW(igrid, tgrid, ygrid)], k)] += expprob*(1.0 - saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JNE_0 < (valueUiNE[inx(isaveWWE[inxW(igrid, tgrid, ygrid)], k)])))?(1.0):(0.0));
                distUiNE[inx(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k)] += expprob*(saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JNE_1 < (valueUiNE[inx(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k)])))?(1.0):(0.0));
                
                distWWEtoUiNE[inxW(igrid, tgrid, ygrid)] += expprob*(1.0 - saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JNE_0 < (valueUiNE[inx(isaveWWE[inxW(igrid, tgrid, ygrid)], k)])))?(1.0):(0.0)) + expprob*(saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JNE_1 < (valueUiNE[inx(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k)])))?(1.0):(0.0));
                #endif
                
                #if SEA == 1
                expprob = mprod[tgrid][k]*myprod[ygrid][y]*(phipar)*etapar[tgrid]*(piE(searchWWE_J[inxW(igrid, tgrid, ygrid)]));

                distUiNE[inx(isaveWWE[inxW(igrid, tgrid, ygrid)], k)] += expprob*(1.0 - saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JNEi_0 < (valueUiNE[inx(isaveWWE[inxW(igrid, tgrid, ygrid)], k)])))?(1.0):(0.0));
                distUiNE[inx(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k)] += expprob*(saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JNEi_1 < (valueUiNE[inx(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k)])))?(1.0):(0.0));
                
                distWWEtoUiNE[inxW(igrid, tgrid, ygrid)] += expprob*(1.0 - saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JNEi_0 < (valueUiNE[inx(isaveWWE[inxW(igrid, tgrid, ygrid)], k)])))?(1.0):(0.0)) + expprob*(saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JNEi_1 < (valueUiNE[inx(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k)])))?(1.0):(0.0));
                #endif
                
                
                // TRANSITION TO WORKER EXCLUDED //
                // not laid off, does not receive phipar, does not receive business idea //
                expprob = mprod[tgrid][k]*myprod[ygrid][y]*(1.0 - phipar)*(1.0 - etapar[tgrid])*(1.0 - piE(searchWWE_J[inxW(igrid, tgrid, ygrid)]));

                distWWE[inxW(isaveWWE[inxW(igrid, tgrid, ygrid)], k, y)] += expprob*(1.0 - saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)];
                distWWE[inxW(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k, y)] += expprob*(saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)];
                
                // not laid off, does not receive phipar, receive business idea //
                expprob = mprod[tgrid][k]*myprod[ygrid][y]*(1.0 - phipar)*(1.0 - etapar[tgrid])*(piE(searchWWE_J[inxW(igrid, tgrid, ygrid)]));

                distWWE[inxW(isaveWWE[inxW(igrid, tgrid, ygrid)], k, y)] += expprob*(1.0 - saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JE_0 <= (valueWWE[inxW(isaveWWE[inxW(igrid, tgrid, ygrid)], k, y)])))?(1.0):(0.0));
                distWWE[inxW(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k, y)] += expprob*(saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JE_1 <= (valueWWE[inxW(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k, y)])))?(1.0):(0.0));
                
                
                // TRANSITION TO WORKER NON-EXCLUDED //
                // not laid off, receive phipar, does not receive business idea //
                expprob = mprod[tgrid][k]*myprod[ygrid][y]*(phipar)*(1.0 - etapar[tgrid])*(1.0 - piE(searchWWE_J[inxW(igrid, tgrid, ygrid)]));

                distWWNE[inxW(isaveWWE[inxW(igrid, tgrid, ygrid)], k, y)] += expprob*(1.0 - saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)];
                distWWNE[inxW(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k, y)] += expprob*(saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)];
                
                // not laid off, receive phipar, receive business idea //
                expprob = mprod[tgrid][k]*myprod[ygrid][y]*(phipar)*(1.0 - etapar[tgrid])*(piE(searchWWE_J[inxW(igrid, tgrid, ygrid)]));

                distWWNE[inxW(isaveWWE[inxW(igrid, tgrid, ygrid)], k, y)] += expprob*(1.0 - saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JNE_0 <= (valueWWNE[inxW(isaveWWE[inxW(igrid, tgrid, ygrid)], k, y)])))?(1.0):(0.0));
                distWWNE[inxW(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k, y)] += expprob*(saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JNE_1 <= (valueWWNE[inxW(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k, y)])))?(1.0):(0.0));
                
                
                
                /*** NON-EXCLUDED ***/
                
                // TRANSITION TO UNEMPLOYED NON EXCLUDED WITH INSURANCE
                // is laid off, does not receive business idea //
                expprob = mprod[tgrid][k]*myprod[ygrid][y]*etapar[tgrid]*(1.0 - piE(searchWWNE_J[inxW(igrid, tgrid, ygrid)]));

                distUiNE[inx(isaveWWNE[inxW(igrid, tgrid, ygrid)], k)] += expprob*(1.0 - saveresWWNE[inxW(igrid, tgrid, ygrid)])*distWWNEold[inxW(igrid, tgrid, ygrid)];
                distUiNE[inx(min((maxgrid-1),(isaveWWNE[inxW(igrid, tgrid, ygrid)]+1)), k)] += expprob*(saveresWWNE[inxW(igrid, tgrid, ygrid)])*distWWNEold[inxW(igrid, tgrid, ygrid)];
                
                distWWNEtoUiNE[inxW(igrid, tgrid, ygrid)] += expprob*(1.0 - saveresWWNE[inxW(igrid, tgrid, ygrid)])*distWWNEold[inxW(igrid, tgrid, ygrid)] +expprob*(saveresWWNE[inxW(igrid, tgrid, ygrid)])*distWWNEold[inxW(igrid, tgrid, ygrid)];
                
                
                // is laid off, does receive business idea //
                #if NOPOLICY == 1
                expprob = mprod[tgrid][k]*myprod[ygrid][y]*etapar[tgrid]*(piE(searchWWNE_J[inxW(igrid, tgrid, ygrid)]));

                distUiNE[inx(isaveWWNE[inxW(igrid, tgrid, ygrid)], k)] += expprob*(1.0 - saveresWWNE[inxW(igrid, tgrid, ygrid)])*distWWNEold[inxW(igrid, tgrid, ygrid)]*(((tempWWNE_JNE_0 < (valueUiNE[inx(isaveWWNE[inxW(igrid, tgrid, ygrid)], k)])))?(1.0):(0.0));
                distUiNE[inx(min((maxgrid-1),(isaveWWNE[inxW(igrid, tgrid, ygrid)]+1)), k)] += expprob*(saveresWWNE[inxW(igrid, tgrid, ygrid)])*distWWNEold[inxW(igrid, tgrid, ygrid)]*(((tempWWNE_JNE_1 < (valueUiNE[inx(min((maxgrid-1),(isaveWWNE[inxW(igrid, tgrid, ygrid)]+1)), k)])))?(1.0):(0.0));
                
                distWWNEtoUiNE[inxW(igrid, tgrid, ygrid)] += expprob*(1.0 - saveresWWNE[inxW(igrid, tgrid, ygrid)])*distWWNEold[inxW(igrid, tgrid, ygrid)]*(((tempWWNE_JNE_0 < (valueUiNE[inx(isaveWWNE[inxW(igrid, tgrid, ygrid)], k)])))?(1.0):(0.0)) + expprob*(saveresWWNE[inxW(igrid, tgrid, ygrid)])*distWWNEold[inxW(igrid, tgrid, ygrid)]*(((tempWWNE_JNE_1 < (valueUiNE[inx(min((maxgrid-1),(isaveWWNE[inxW(igrid, tgrid, ygrid)]+1)), k)])))?(1.0):(0.0));
                #endif
                
                #if SEA == 1
                expprob = mprod[tgrid][k]*myprod[ygrid][y]*etapar[tgrid]*(piE(searchWWNE_J[inxW(igrid, tgrid, ygrid)]));

                distUiNE[inx(isaveWWNE[inxW(igrid, tgrid, ygrid)], k)] += expprob*(1.0 - saveresWWNE[inxW(igrid, tgrid, ygrid)])*distWWNEold[inxW(igrid, tgrid, ygrid)]*(((tempWWNE_JNEi_0 < (valueUiNE[inx(isaveWWNE[inxW(igrid, tgrid, ygrid)], k)])))?(1.0):(0.0));
                distUiNE[inx(min((maxgrid-1),(isaveWWNE[inxW(igrid, tgrid, ygrid)]+1)), k)] += expprob*(saveresWWNE[inxW(igrid, tgrid, ygrid)])*distWWNEold[inxW(igrid, tgrid, ygrid)]*(((tempWWNE_JNEi_1 < (valueUiNE[inx(min((maxgrid-1),(isaveWWNE[inxW(igrid, tgrid, ygrid)]+1)), k)])))?(1.0):(0.0));
                
                distWWNEtoUiNE[inxW(igrid, tgrid, ygrid)] += expprob*(1.0 - saveresWWNE[inxW(igrid, tgrid, ygrid)])*distWWNEold[inxW(igrid, tgrid, ygrid)]*(((tempWWNE_JNEi_0 < (valueUiNE[inx(isaveWWNE[inxW(igrid, tgrid, ygrid)], k)])))?(1.0):(0.0)) + expprob*(saveresWWNE[inxW(igrid, tgrid, ygrid)])*distWWNEold[inxW(igrid, tgrid, ygrid)]*(((tempWWNE_JNEi_1 < (valueUiNE[inx(min((maxgrid-1),(isaveWWNE[inxW(igrid, tgrid, ygrid)]+1)), k)])))?(1.0):(0.0));
                #endif
                
                
                // TRANSITION TO WORKER NON-EXCLUDED //
                // not laid off, receive phipar, does not receive business idea //
                expprob = mprod[tgrid][k]*myprod[ygrid][y]*(1.0 - etapar[tgrid])*(1.0 - piE(searchWWNE_J[inxW(igrid, tgrid, ygrid)]));

                distWWNE[inxW(isaveWWNE[inxW(igrid, tgrid, ygrid)], k, y)] += expprob*(1.0 - saveresWWNE[inxW(igrid, tgrid, ygrid)])*distWWNEold[inxW(igrid, tgrid, ygrid)];
                distWWNE[inxW(min((maxgrid-1),(isaveWWNE[inxW(igrid, tgrid, ygrid)]+1)), k, y)] += expprob*(saveresWWNE[inxW(igrid, tgrid, ygrid)])*distWWNEold[inxW(igrid, tgrid, ygrid)];
                
            
                // not laid off, receive phipar, receive business idea //
                expprob = mprod[tgrid][k]*myprod[ygrid][y]*(1.0 - etapar[tgrid])*(piE(searchWWNE_J[inxW(igrid, tgrid, ygrid)]));

                distWWNE[inxW(isaveWWNE[inxW(igrid, tgrid, ygrid)], k, y)] += expprob*(1.0 - saveresWWNE[inxW(igrid, tgrid, ygrid)])*distWWNEold[inxW(igrid, tgrid, ygrid)]*(((tempWWNE_JNE_0 <= (valueWWNE[inxW(isaveWWNE[inxW(igrid, tgrid, ygrid)], k, y)])))?(1.0):(0.0));
                distWWNE[inxW(min((maxgrid-1),(isaveWWNE[inxW(igrid, tgrid, ygrid)]+1)), k, y)] += expprob*(saveresWWNE[inxW(igrid, tgrid, ygrid)])*distWWNEold[inxW(igrid, tgrid, ygrid)]*(((tempWWNE_JNE_1 <= (valueWWNE[inxW(min((maxgrid-1),(isaveWWNE[inxW(igrid, tgrid, ygrid)]+1)), k, y)])))?(1.0):(0.0));
                
                
                
                
                // WHEN RECEIVE OPPORTUNITY //
                for(e = 0; e<maxfirmtype; e++) {
                
                
                    /** EXCLUDED **/
                    
                    // TRANSITION TO ENTREPRENEUR EXCLUDED //
                    // not laid off, not receive phipar, receive business idea //
                    expprob = mzinv[e]*mprod[tgrid][k]*myprod[ygrid][y]*(1.0 - phipar)*(1.0 - etapar[tgrid])*(piE(searchWWE_J[inxW(igrid, tgrid, ygrid)]));

                    distJE[inxE(isaveWWE[inxW(igrid, tgrid, ygrid)], k, e)] += expprob*(1.0 - saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JE_0 > (valueWWE[inxW(isaveWWE[inxW(igrid, tgrid, ygrid)], k, y)])))?(1.0):(0.0));
                    distJE[inxE(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k, e)] += expprob*(saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JE_1 > (valueWWE[inxW(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k, y)])))?(1.0):(0.0));
                    
                    distWWEtoJE[inxW(igrid, tgrid, ygrid)] += expprob*(1.0 - saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JE_0 > (valueWWE[inxW(isaveWWE[inxW(igrid, tgrid, ygrid)], k, y)])))?(1.0):(0.0)) + expprob*(saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JE_1 > (valueWWE[inxW(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k, y)])))?(1.0):(0.0));
      
                    
                    // laid off, not receive phipar, receive business idea //
                    #if NOPOLICY == 1
                        expprob = mzinv[e]*mprod[tgrid][k]*myprod[ygrid][y]*(1.0 - phipar)*(etapar[tgrid])*(piE(searchWWE_J[inxW(igrid, tgrid, ygrid)]));

                        distJE[inxE(isaveWWE[inxW(igrid, tgrid, ygrid)], k, e)] += expprob*(1.0 - saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JE_0 >= (valueUiE[inx(isaveWWE[inxW(igrid, tgrid, ygrid)], k)])))?(1.0):(0.0));
                        distJE[inxE(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k, e)] += expprob*(saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JE_1 >= (valueUiE[inx(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k)])))?(1.0):(0.0));
                        
                        distWWEtoJE[inxW(igrid, tgrid, ygrid)] += expprob*(1.0 - saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JE_0 >= (valueUiE[inx(isaveWWE[inxW(igrid, tgrid, ygrid)], k)])))?(1.0):(0.0)) + expprob*(saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JE_1 >= (valueUiE[inx(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k)])))?(1.0):(0.0));
                        
                        distWWEfromUiEtoJE[inx(igrid, tgrid)] += expprob*(1.0 - saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JE_0 >= (valueUiE[inx(isaveWWE[inxW(igrid, tgrid, ygrid)], k)])))?(1.0):(0.0)) + expprob*(saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JE_1 >= (valueUiE[inx(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k)])))?(1.0):(0.0));

                        
                        if(valueUiE[inx(isaveWWE[inxW(igrid, tgrid, ygrid)], k)] <= tempWWE_JE_0 & valueWWE[inxW(isaveWWE[inxW(igrid, tgrid, ygrid)], k, y)] >= tempWWE_JE_0) {
                            necessityWWE[inxW(igrid, tgrid, ygrid)]  += expprob*(1.0 - saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JE_0 >= (valueUiE[inx(isaveWWE[inxW(igrid, tgrid, ygrid)], k)])))?(1.0):(0.0));
                            }
                        if(valueUiE[inx(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k)] <= tempWWE_JE_1 & valueWWE[inxW(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k, y)] >= tempWWE_JE_1) {
                            necessityWWE[inxW(igrid, tgrid, ygrid)]  += expprob*(saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JE_1 >= (valueUiE[inx(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k)])))?(1.0):(0.0));
                            }
                    #endif
                    
        
                    #if SEA == 1
                        expprob = mzinv[e]*mprod[tgrid][k]*myprod[ygrid][y]*(1.0 - phipar)*(etapar[tgrid])*(piE(searchWWE_J[inxW(igrid, tgrid, ygrid)]));

                        distJEi[inxE(isaveWWE[inxW(igrid, tgrid, ygrid)], k, e)] += expprob*(1.0 - saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JEi_0 >= (valueUiE[inx(isaveWWE[inxW(igrid, tgrid, ygrid)], k)])))?(1.0):(0.0));
                        distJEi[inxE(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k, e)] += expprob*(saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JEi_1 >= (valueUiE[inx(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k)])))?(1.0):(0.0));
                        
                        distWWEtoJE[inxW(igrid, tgrid, ygrid)] += expprob*(1.0 - saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JEi_0 >= (valueUiE[inx(isaveWWE[inxW(igrid, tgrid, ygrid)], k)])))?(1.0):(0.0)) + expprob*(saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JEi_1 >= (valueUiE[inx(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k)])))?(1.0):(0.0));
                        
                        distWWEfromUiEtoJE[inx(igrid, tgrid)] += expprob*(1.0 - saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JEi_0 >= (valueUiE[inx(isaveWWE[inxW(igrid, tgrid, ygrid)], k)])))?(1.0):(0.0)) + expprob*(saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JEi_1 >= (valueUiE[inx(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k)])))?(1.0):(0.0));


                        if(tempWWE_JEi_0 >= (valueUiE[inx(isaveWWE[inxW(igrid, tgrid, ygrid)], k)]) & tempWWE_JE_0 <= (valueUiE[inx(isaveWWE[inxW(igrid, tgrid, ygrid)], k)])) {
                            distUiEtoJE_select[inx(igrid, tgrid)] += expprob*(1.0 - saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JEi_0 >= (valueUiE[inx(isaveWWE[inxW(igrid, tgrid, ygrid)], k)])))?(1.0):(0.0));
                        }
                        if(tempWWE_JEi_1 >= (valueUiE[inx(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k)]) & tempWWE_JE_1 <= (valueUiE[inx(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k)])) {
                            distUiEtoJE_select[inx(igrid, tgrid)] += expprob*(saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JEi_1 >= (valueUiE[inx(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k)])))?(1.0):(0.0));
                        }
                        
                        
                        if(valueUiE[inx(isaveWWE[inxW(igrid, tgrid, ygrid)], k)] <= (tempWWE_JEi_0*(1.0-alt_def_necessity_SEA) + alt_def_necessity_SEA*tempWWE_JE_0) & valueWWE[inxW(isaveWWE[inxW(igrid, tgrid, ygrid)], k, y)] >= (tempWWE_JEi_0*(1.0-alt_def_necessity_SEA) + alt_def_necessity_SEA*tempWWE_JE_0)) {
                            necessityWWE[inxW(igrid, tgrid, ygrid)]  += expprob*(1.0 - saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*((((tempWWE_JEi_0*(1.0-alt_def_necessity_SEA) + alt_def_necessity_SEA*tempWWE_JE_0) >= (valueUiE[inx(isaveWWE[inxW(igrid, tgrid, ygrid)], k)])))?(1.0):(0.0));
                            }
                        if(valueUiE[inx(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k)] <= (tempWWE_JEi_1*(1.0-alt_def_necessity_SEA) + alt_def_necessity_SEA*tempWWE_JE_1) & valueWWE[inxW(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k, y)] >= (tempWWE_JEi_1*(1.0-alt_def_necessity_SEA) + alt_def_necessity_SEA*tempWWE_JE_1)) {
                            necessityWWE[inxW(igrid, tgrid, ygrid)]  += expprob*(saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*((((tempWWE_JEi_1*(1.0-alt_def_necessity_SEA) + alt_def_necessity_SEA*tempWWE_JE_1) >= (valueUiE[inx(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k)])))?(1.0):(0.0));
                            }

                    #endif
                    


                    // TRANSITION TO ENTREPRENEUR NON-EXCLUDED //
                    // not laid off, receive phipar, receive business idea //
                    expprob = mzinv[e]*mprod[tgrid][k]*myprod[ygrid][y]*(phipar)*(1.0 - etapar[tgrid])*(piE(searchWWE_J[inxW(igrid, tgrid, ygrid)]));

                    distJNE[inxE(isaveWWE[inxW(igrid, tgrid, ygrid)], k, e)] += expprob*(1.0 - saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JNE_0 > (valueWWNE[inxW(isaveWWE[inxW(igrid, tgrid, ygrid)], k, y)])))?(1.0):(0.0));
                    distJNE[inxE(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k, e)] += expprob*(saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JNE_1 > (valueWWNE[inxW(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k, y)])))?(1.0):(0.0));
                    
                    distWWEtoJNE[inxW(igrid, tgrid, ygrid)] += expprob*(1.0 - saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JNE_0 > (valueWWNE[inxW(isaveWWE[inxW(igrid, tgrid, ygrid)], k, y)])))?(1.0):(0.0)) + expprob*(saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JNE_1 > (valueWWNE[inxW(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k, y)])))?(1.0):(0.0));
                    
                    
                    // laid off, receive phipar, receive business idea //
                    #if NOPOLICY == 1
                        expprob = mzinv[e]*mprod[tgrid][k]*myprod[ygrid][y]*(phipar)*(etapar[tgrid])*(piE(searchWWE_J[inxW(igrid, tgrid, ygrid)]));

                        distJNE[inxE(isaveWWE[inxW(igrid, tgrid, ygrid)], k, e)] += expprob*(1.0 - saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JNE_0 >= (valueUiNE[inx(isaveWWE[inxW(igrid, tgrid, ygrid)], k)])))?(1.0):(0.0));
                        distJNE[inxE(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k, e)] += expprob*(saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JNE_1 >= (valueUiNE[inx(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k)])))?(1.0):(0.0));
                        
                        distWWEtoJNE[inxW(igrid, tgrid, ygrid)] += expprob*(1.0 - saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JNE_0 >= (valueUiNE[inx(isaveWWE[inxW(igrid, tgrid, ygrid)], k)])))?(1.0):(0.0)) + expprob*(saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JNE_1 >= (valueUiNE[inx(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k)])))?(1.0):(0.0));
                        
                        distWWEfromUiEtoJNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JNE_0 >= (valueUiNE[inx(isaveWWE[inxW(igrid, tgrid, ygrid)], k)])))?(1.0):(0.0)) + expprob*(saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JNE_1 >= (valueUiNE[inx(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k)])))?(1.0):(0.0));
                        
                        if(valueUiNE[inx(isaveWWE[inxW(igrid, tgrid, ygrid)], k)] <= tempWWE_JNE_0 & valueWWNE[inxW(isaveWWE[inxW(igrid, tgrid, ygrid)], k, y)] >= tempWWE_JNE_0) {
                            necessityWWE[inxW(igrid, tgrid, ygrid)]  += expprob*(1.0 - saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JNE_0 >= (valueUiNE[inx(isaveWWE[inxW(igrid, tgrid, ygrid)], k)])))?(1.0):(0.0));
                            }
                        if(valueUiNE[inx(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k)] <= tempWWE_JNE_1 & valueWWNE[inxW(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k, y)] >= tempWWE_JNE_1) {
                            necessityWWE[inxW(igrid, tgrid, ygrid)]  += expprob*(saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JNE_1 >= (valueUiNE[inx(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k)])))?(1.0):(0.0));
                            }
                    #endif
                    
                    
                    #if SEA == 1
                        expprob = mzinv[e]*mprod[tgrid][k]*myprod[ygrid][y]*(phipar)*(etapar[tgrid])*(piE(searchWWE_J[inxW(igrid, tgrid, ygrid)]));

                        distJNEi[inxE(isaveWWE[inxW(igrid, tgrid, ygrid)], k, e)] += expprob*(1.0 - saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JNEi_0 >= (valueUiNE[inx(isaveWWE[inxW(igrid, tgrid, ygrid)], k)])))?(1.0):(0.0));
                        distJNEi[inxE(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k, e)] += expprob*(saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JNEi_1 >= (valueUiNE[inx(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k)])))?(1.0):(0.0));
                        
                        distWWEtoJNE[inxW(igrid, tgrid, ygrid)] += expprob*(1.0 - saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JNEi_0 >= (valueUiNE[inx(isaveWWE[inxW(igrid, tgrid, ygrid)], k)])))?(1.0):(0.0)) + expprob*(saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JNEi_1 >= (valueUiNE[inx(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k)])))?(1.0):(0.0));
                        
                        distWWEfromUiEtoJNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JNEi_0 >= (valueUiNE[inx(isaveWWE[inxW(igrid, tgrid, ygrid)], k)])))?(1.0):(0.0)) + expprob*(saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JNEi_1 >= (valueUiNE[inx(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k)])))?(1.0):(0.0));
                        
                        if(tempWWE_JNEi_0 >= (valueUiNE[inx(isaveWWE[inxW(igrid, tgrid, ygrid)], k)]) & tempWWE_JNE_0 <= (valueUiNE[inx(isaveWWE[inxW(igrid, tgrid, ygrid)], k)])) {
                            distUiEtoJE_select[inx(igrid, tgrid)] += expprob*(1.0 - saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JNEi_0 >= (valueUiNE[inx(isaveWWE[inxW(igrid, tgrid, ygrid)], k)])))?(1.0):(0.0));
                        }
                        if(tempWWE_JNEi_1 >= (valueUiNE[inx(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k)]) & tempWWE_JNE_1 <= (valueUiNE[inx(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k)])) {
                            distUiEtoJE_select[inx(igrid, tgrid)] += expprob*(saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*(((tempWWE_JNEi_1 >= (valueUiNE[inx(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k)])))?(1.0):(0.0));
                        }
                        
                        if(valueUiNE[inx(isaveWWE[inxW(igrid, tgrid, ygrid)], k)] <= (tempWWE_JNEi_0*(1.0-alt_def_necessity_SEA) + alt_def_necessity_SEA*tempWWE_JNE_0) & valueWWNE[inxW(isaveWWE[inxW(igrid, tgrid, ygrid)], k, y)] >= (tempWWE_JNEi_0*(1.0-alt_def_necessity_SEA) + alt_def_necessity_SEA*tempWWE_JNE_0)) {
                            necessityWWE[inxW(igrid, tgrid, ygrid)]  += expprob*(1.0 - saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*((((tempWWE_JNEi_0*(1.0-alt_def_necessity_SEA) + alt_def_necessity_SEA*tempWWE_JNE_0) >= (valueUiNE[inx(isaveWWE[inxW(igrid, tgrid, ygrid)], k)])))?(1.0):(0.0));
                            }
                        if(valueUiNE[inx(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k)] <= (tempWWE_JNEi_1*(1.0-alt_def_necessity_SEA) + alt_def_necessity_SEA*tempWWE_JNE_1) & valueWWNE[inxW(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k, y)] >= (tempWWE_JNEi_1*(1.0-alt_def_necessity_SEA) + alt_def_necessity_SEA*tempWWE_JNE_1)) {
                            necessityWWE[inxW(igrid, tgrid, ygrid)]  += expprob*(saveresWWE[inxW(igrid, tgrid, ygrid)])*distWWEold[inxW(igrid, tgrid, ygrid)]*((((tempWWE_JNEi_1*(1.0-alt_def_necessity_SEA) + alt_def_necessity_SEA*tempWWE_JNE_1) >= (valueUiNE[inx(min((maxgrid-1),(isaveWWE[inxW(igrid, tgrid, ygrid)]+1)), k)])))?(1.0):(0.0));
                            }
                    #endif
                    
                    

                    /*** NON-EXCLUDED ***/
                    
                    // TRANSITION TO ENTREPRENEUR NON-EXCLUDED //
                    // not laid off, receive business idea //
                    expprob = mzinv[e]*mprod[tgrid][k]*myprod[ygrid][y]*(1.0 - etapar[tgrid])*(piE(searchWWNE_J[inxW(igrid, tgrid, ygrid)]));

                    distJNE[inxE(isaveWWNE[inxW(igrid, tgrid, ygrid)], k, e)] += expprob*(1.0 - saveresWWNE[inxW(igrid, tgrid, ygrid)])*distWWNEold[inxW(igrid, tgrid, ygrid)]*(((tempWWNE_JNE_0 > (valueWWNE[inxW(isaveWWNE[inxW(igrid, tgrid, ygrid)], k, y)])))?(1.0):(0.0));
                    distJNE[inxE(min((maxgrid-1),(isaveWWNE[inxW(igrid, tgrid, ygrid)]+1)), k, e)] += expprob*(saveresWWNE[inxW(igrid, tgrid, ygrid)])*distWWNEold[inxW(igrid, tgrid, ygrid)]*(((tempWWNE_JNE_1 > (valueWWNE[inxW(min((maxgrid-1),(isaveWWNE[inxW(igrid, tgrid, ygrid)]+1)), k, y)])))?(1.0):(0.0));
                    
                    distWWNEtoJNE[inxW(igrid, tgrid, ygrid)] += expprob*(1.0 - saveresWWNE[inxW(igrid, tgrid, ygrid)])*distWWNEold[inxW(igrid, tgrid, ygrid)]*(((tempWWNE_JNE_0 > (valueWWNE[inxW(isaveWWNE[inxW(igrid, tgrid, ygrid)], k, y)])))?(1.0):(0.0)) + expprob*(saveresWWNE[inxW(igrid, tgrid, ygrid)])*distWWNEold[inxW(igrid, tgrid, ygrid)]*(((tempWWNE_JNE_1 > (valueWWNE[inxW(min((maxgrid-1),(isaveWWNE[inxW(igrid, tgrid, ygrid)]+1)), k, y)])))?(1.0):(0.0));
                    
                    
                    // laid off, receive phipar, receive business idea //
                    #if NOPOLICY == 1
                        expprob = mzinv[e]*mprod[tgrid][k]*myprod[ygrid][y]*(etapar[tgrid])*(piE(searchWWNE_J[inxW(igrid, tgrid, ygrid)]));

                        distJNE[inxE(isaveWWNE[inxW(igrid, tgrid, ygrid)], k, e)] += expprob*(1.0 - saveresWWNE[inxW(igrid, tgrid, ygrid)])*distWWNEold[inxW(igrid, tgrid, ygrid)]*(((tempWWNE_JNE_0 >= (valueUiNE[inx(isaveWWNE[inxW(igrid, tgrid, ygrid)], k)])))?(1.0):(0.0));
                        distJNE[inxE(min((maxgrid-1),(isaveWWNE[inxW(igrid, tgrid, ygrid)]+1)), k, e)] += expprob*(saveresWWNE[inxW(igrid, tgrid, ygrid)])*distWWNEold[inxW(igrid, tgrid, ygrid)]*(((tempWWNE_JNE_1 >= (valueUiNE[inx(min((maxgrid-1),(isaveWWNE[inxW(igrid, tgrid, ygrid)]+1)), k)])))?(1.0):(0.0));
                        
                        distWWNEtoJNE[inxW(igrid, tgrid, ygrid)] += expprob*(1.0 - saveresWWNE[inxW(igrid, tgrid, ygrid)])*distWWNEold[inxW(igrid, tgrid, ygrid)]*(((tempWWNE_JNE_0 >= (valueUiNE[inx(isaveWWNE[inxW(igrid, tgrid, ygrid)], k)])))?(1.0):(0.0)) + expprob*(saveresWWNE[inxW(igrid, tgrid, ygrid)])*distWWNEold[inxW(igrid, tgrid, ygrid)]*(((tempWWNE_JNE_1 >= (valueUiNE[inx(min((maxgrid-1),(isaveWWNE[inxW(igrid, tgrid, ygrid)]+1)), k)])))?(1.0):(0.0));
                        
                        distWWNEfromUiNEtoJNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresWWNE[inxW(igrid, tgrid, ygrid)])*distWWNEold[inxW(igrid, tgrid, ygrid)]*(((tempWWNE_JNE_0 >= (valueUiNE[inx(isaveWWNE[inxW(igrid, tgrid, ygrid)], k)])))?(1.0):(0.0)) + expprob*(saveresWWNE[inxW(igrid, tgrid, ygrid)])*distWWNEold[inxW(igrid, tgrid, ygrid)]*(((tempWWNE_JNE_1 >= (valueUiNE[inx(min((maxgrid-1),(isaveWWNE[inxW(igrid, tgrid, ygrid)]+1)), k)])))?(1.0):(0.0));
                        
                        if(valueUiNE[inx(isaveWWNE[inxW(igrid, tgrid, ygrid)], k)] <= tempWWNE_JNE_0 & valueWWNE[inxW(isaveWWNE[inxW(igrid, tgrid, ygrid)], k, y)] >= tempWWNE_JNE_0) {
                            necessityWWNE[inxW(igrid, tgrid, ygrid)] += expprob*(1.0 - saveresWWNE[inxW(igrid, tgrid, ygrid)])*distWWNEold[inxW(igrid, tgrid, ygrid)]*(((tempWWNE_JNE_0 >= (valueUiNE[inx(isaveWWNE[inxW(igrid, tgrid, ygrid)], k)])))?(1.0):(0.0));
                        }
                        if(valueUiNE[inx(min((maxgrid-1),(isaveWWNE[inxW(igrid, tgrid, ygrid)]+1)), k)] <= tempWWNE_JNE_1 & valueWWNE[inxW(min((maxgrid-1),(isaveWWNE[inxW(igrid, tgrid, ygrid)]+1)), k, y)] >= tempWWNE_JNE_1) {
                            necessityWWNE[inxW(igrid, tgrid, ygrid)] += expprob*(saveresWWNE[inxW(igrid, tgrid, ygrid)])*distWWNEold[inxW(igrid, tgrid, ygrid)]*(((tempWWNE_JNE_1 >= (valueUiNE[inx(min((maxgrid-1),(isaveWWNE[inxW(igrid, tgrid, ygrid)]+1)), k)])))?(1.0):(0.0));
                        }
                    #endif
                    
                    
                    #if SEA == 1
                        expprob = mzinv[e]*mprod[tgrid][k]*myprod[ygrid][y]*(etapar[tgrid])*(piE(searchWWNE_J[inxW(igrid, tgrid, ygrid)]));

                        distJNEi[inxE(isaveWWNE[inxW(igrid, tgrid, ygrid)], k, e)] += expprob*(1.0 - saveresWWNE[inxW(igrid, tgrid, ygrid)])*distWWNEold[inxW(igrid, tgrid, ygrid)]*(((tempWWNE_JNEi_0 >= (valueUiNE[inx(isaveWWNE[inxW(igrid, tgrid, ygrid)], k)])))?(1.0):(0.0));
                        distJNEi[inxE(min((maxgrid-1),(isaveWWNE[inxW(igrid, tgrid, ygrid)]+1)), k, e)] += expprob*(saveresWWNE[inxW(igrid, tgrid, ygrid)])*distWWNEold[inxW(igrid, tgrid, ygrid)]*(((tempWWNE_JNEi_1 >= (valueUiNE[inx(min((maxgrid-1),(isaveWWNE[inxW(igrid, tgrid, ygrid)]+1)), k)])))?(1.0):(0.0));
                        
                        distWWNEtoJNE[inxW(igrid, tgrid, ygrid)] += expprob*(1.0 - saveresWWNE[inxW(igrid, tgrid, ygrid)])*distWWNEold[inxW(igrid, tgrid, ygrid)]*(((tempWWNE_JNEi_0 >= (valueUiNE[inx(isaveWWNE[inxW(igrid, tgrid, ygrid)], k)])))?(1.0):(0.0)) + expprob*(saveresWWNE[inxW(igrid, tgrid, ygrid)])*distWWNEold[inxW(igrid, tgrid, ygrid)]*(((tempWWNE_JNEi_1 >= (valueUiNE[inx(min((maxgrid-1),(isaveWWNE[inxW(igrid, tgrid, ygrid)]+1)), k)])))?(1.0):(0.0));
                        
                        distWWNEfromUiNEtoJNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresWWNE[inxW(igrid, tgrid, ygrid)])*distWWNEold[inxW(igrid, tgrid, ygrid)]*(((tempWWNE_JNEi_0 >= (valueUiNE[inx(isaveWWNE[inxW(igrid, tgrid, ygrid)], k)])))?(1.0):(0.0)) + expprob*(saveresWWNE[inxW(igrid, tgrid, ygrid)])*distWWNEold[inxW(igrid, tgrid, ygrid)]*(((tempWWNE_JNEi_1 >= (valueUiNE[inx(min((maxgrid-1),(isaveWWNE[inxW(igrid, tgrid, ygrid)]+1)), k)])))?(1.0):(0.0));
                        
                        if(tempWWNE_JNEi_0 >= (valueUiNE[inx(isaveWWNE[inxW(igrid, tgrid, ygrid)], k)]) & tempWWNE_JNE_0 <= (valueUiNE[inx(isaveWWNE[inxW(igrid, tgrid, ygrid)], k)])) {
                            distUiEtoJE_select[inx(igrid, tgrid)] += expprob*(1.0 - saveresWWNE[inxW(igrid, tgrid, ygrid)])*distWWNEold[inxW(igrid, tgrid, ygrid)]*(((tempWWNE_JNEi_0 >= (valueUiNE[inx(isaveWWNE[inxW(igrid, tgrid, ygrid)], k)])))?(1.0):(0.0));
                        }
                        if(tempWWNE_JNEi_1 >= (valueUiNE[inx(min((maxgrid-1),(isaveWWNE[inxW(igrid, tgrid, ygrid)]+1)), k)]) & tempWWNE_JNE_1 <= (valueUiNE[inx(min((maxgrid-1),(isaveWWNE[inxW(igrid, tgrid, ygrid)]+1)), k)])) {
                            distUiEtoJE_select[inx(igrid, tgrid)] += expprob*(saveresWWNE[inxW(igrid, tgrid, ygrid)])*distWWNEold[inxW(igrid, tgrid, ygrid)]*(((tempWWNE_JNEi_1 >= (valueUiNE[inx(min((maxgrid-1),(isaveWWNE[inxW(igrid, tgrid, ygrid)]+1)), k)])))?(1.0):(0.0));
                        }
                        
                        if(valueUiNE[inx(isaveWWNE[inxW(igrid, tgrid, ygrid)], k)] <= (tempWWNE_JNEi_0*(1.0-alt_def_necessity_SEA) + alt_def_necessity_SEA*tempWWNE_JNE_0) & valueWWNE[inxW(isaveWWNE[inxW(igrid, tgrid, ygrid)], k, y)] >= (tempWWNE_JNEi_0*(1.0-alt_def_necessity_SEA) + alt_def_necessity_SEA*tempWWNE_JNE_0)) {
                            necessityWWNE[inxW(igrid, tgrid, ygrid)] += expprob*(1.0 - saveresWWNE[inxW(igrid, tgrid, ygrid)])*distWWNEold[inxW(igrid, tgrid, ygrid)]*((((tempWWNE_JNEi_0*(1.0-alt_def_necessity_SEA) + alt_def_necessity_SEA*tempWWNE_JNE_0) >= (valueUiNE[inx(isaveWWNE[inxW(igrid, tgrid, ygrid)], k)])))?(1.0):(0.0));
                        }
                        if(valueUiNE[inx(min((maxgrid-1),(isaveWWNE[inxW(igrid, tgrid, ygrid)]+1)), k)] <= (tempWWNE_JNEi_1*(1.0-alt_def_necessity_SEA) + alt_def_necessity_SEA*tempWWNE_JNE_1) & valueWWNE[inxW(min((maxgrid-1),(isaveWWNE[inxW(igrid, tgrid, ygrid)]+1)), k, y)] >= (tempWWNE_JNEi_1*(1.0-alt_def_necessity_SEA) + alt_def_necessity_SEA*tempWWNE_JNE_1)) {
                            necessityWWNE[inxW(igrid, tgrid, ygrid)] += expprob*(saveresWWNE[inxW(igrid, tgrid, ygrid)])*distWWNEold[inxW(igrid, tgrid, ygrid)]*((((tempWWNE_JNEi_1*(1.0-alt_def_necessity_SEA) + alt_def_necessity_SEA*tempWWNE_JNE_1) >= (valueUiNE[inx(min((maxgrid-1),(isaveWWNE[inxW(igrid, tgrid, ygrid)]+1)), k)])))?(1.0):(0.0));
                        }
                    #endif
                    
                } // end e grid
            } // end y grid
        } // end ygrid grid
        
        
        
        
        
        /******************/
        /*** UNEMPLOYED ***/
        /******************/
    
        
        // COMPUTE THE EXPECTED NEXT TIME ENTREPRENEURIAL VALUE //
        tempUiE_JE_0 = 0.0;tempUnE_JE_0 = 0.0;tempUiE_JE_1 = 0.0;tempUnE_JE_1 = 0.0;tempUiE_JNE_0 = 0.0;tempUnE_JNE_0 = 0.0;tempUiE_JNE_1 = 0.0;tempUnE_JNE_1 = 0.0;tempUiNE_JNE_0 = 0.0;tempUnNE_JNE_0 = 0.0;tempUiNE_JNE_1 = 0.0;tempUnNE_JNE_1 = 0.0;
        
        #if SEA == 1
            tempUiE_JEi_0 = 0.0;tempUiE_JEi_1 = 0.0;tempUiE_JNEi_0 = 0.0;tempUiE_JNEi_1 = 0.0;tempUiNE_JNEi_0 = 0.0;tempUiNE_JNEi_1 = 0.0;
        #endif
        
        for(e = 0; e < maxfirmtype; e++) {
            tempUnE_JE_0    += mzinv[e]*valueJE[inxE(isaveUnE[inx(igrid, tgrid)], k, e)];
            tempUnE_JE_1    += mzinv[e]*valueJE[inxE(min((maxgrid-1),(isaveUnE[inx(igrid, tgrid)]+1)), k, e)];
            tempUnE_JNE_0   += mzinv[e]*valueJNE[inxE(isaveUnE[inx(igrid, tgrid)], k, e)];
            tempUnE_JNE_1   += mzinv[e]*valueJNE[inxE(min((maxgrid-1),(isaveUnE[inx(igrid, tgrid)]+1)), k, e)];
            tempUnNE_JNE_0  += mzinv[e]*valueJNE[inxE(isaveUnNE[inx(igrid, tgrid)], k, e)];
            tempUnNE_JNE_1  += mzinv[e]*valueJNE[inxE(min((maxgrid-1),(isaveUnNE[inx(igrid, tgrid)]+1)), k, e)];
        
            #if NOPOLICY == 1
                tempUiE_JE_0    += mzinv[e]*valueJE[inxE(isaveUiE[inx(igrid, tgrid)], k, e)];
                tempUiE_JE_1    += mzinv[e]*valueJE[inxE(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, e)];
                tempUiE_JNE_0   += mzinv[e]*valueJNE[inxE(isaveUiE[inx(igrid, tgrid)], k, e)];
                tempUiE_JNE_1   += mzinv[e]*valueJNE[inxE(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, e)];
                tempUiNE_JNE_0  += mzinv[e]*valueJNE[inxE(isaveUiNE[inx(igrid, tgrid)], k, e)];
                tempUiNE_JNE_1  += mzinv[e]*valueJNE[inxE(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k, e)];
            #endif
            
            #if SEA == 1
                tempUiE_JEi_0   += mzinv[e]*valueJEi[inxE(isaveUiE[inx(igrid, tgrid)], k, e)];
                tempUiE_JEi_1   += mzinv[e]*valueJEi[inxE(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, e)];
                tempUiE_JNEi_0  += mzinv[e]*valueJNEi[inxE(isaveUiE[inx(igrid, tgrid)], k, e)];
                tempUiE_JNEi_1  += mzinv[e]*valueJNEi[inxE(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, e)];
                tempUiNE_JNEi_0 += mzinv[e]*valueJNEi[inxE(isaveUiNE[inx(igrid, tgrid)], k, e)];
                tempUiNE_JNEi_1 += mzinv[e]*valueJNEi[inxE(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k, e)];
            #endif
        }
        
        
        /*** UNEMPLOYED INSURED EXCLUDED ***/

        // TRANSITION TO UNEMPLOYED NOT-INSURED -> NOT EXCLUDED
        // receive pLTpar, does not receive business idea, does not receive job, receive phipar
        expprob = mprod[tgrid][k]*(phipar)*(pLTpar)*(1.0 - piE(searchUiE_J[inx(igrid, tgrid)]))*(1.0 - piW(searchUiE_W[inx(igrid, tgrid)]));
        distUnNE[inx(isaveUiE[inx(igrid, tgrid)], k)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)];
        distUnNE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)];
        distUiEtoUnNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)] + expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)];
 
        // receive pLTpar, receive business idea, does not receive job, receive phipar
        expprob = mprod[tgrid][k]*(phipar)*(pLTpar)*(piE(searchUiE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUiE_W[inx(igrid, tgrid)]));
        distUnNE[inx(isaveUiE[inx(igrid, tgrid)], k)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_0 < (valueUnNE[inx(isaveUiE[inx(igrid, tgrid)], k)])))?(1.0):(0.0));
        distUnNE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_1 < (valueUnNE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
        distUiEtoUnNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_0 < (valueUnNE[inx(isaveUiE[inx(igrid, tgrid)], k)])))?(1.0):(0.0)) + expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_1 < (valueUnNE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
        
            
        // TRANSITION TO UNEMPLOYED NOT-INSURED -> EXCLUDED
        // receive pLTpar, does not receive business idea, does not receive job, receive phipar
        expprob = mprod[tgrid][k]*(1.0 - phipar)*(pLTpar)*(1.0 - piE(searchUiE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUiE_W[inx(igrid, tgrid)]));
        distUnE[inx(isaveUiE[inx(igrid, tgrid)], k)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)];
        distUnE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)];
        distUiEtoUnE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)] + expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)];
            
        // receive pLTpar, receive business idea, does not receive job, receive phipar
        expprob = mprod[tgrid][k]*(1.0 - phipar)*(pLTpar)*(piE(searchUiE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUiE_W[inx(igrid, tgrid)]));
        distUnE[inx(isaveUiE[inx(igrid, tgrid)], k)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JE_0 < (valueUnE[inx(isaveUiE[inx(igrid, tgrid)], k)])))?(1.0):(0.0));
        distUnE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JE_1 < (valueUnE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
        distUiEtoUnE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JE_0 < (valueUnE[inx(isaveUiE[inx(igrid, tgrid)], k)])))?(1.0):(0.0)) + expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JE_1 < (valueUnE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));


        
        // TRANSITION TO UNEMPLOYED INSURED -> NOT-EXCLUDED
        // receive pLTpar, does not receive business idea, does not receive job, receive phipar
        expprob = mprod[tgrid][k]*(phipar)*(1.0 - pLTpar)*(1.0 - piE(searchUiE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUiE_W[inx(igrid, tgrid)]));
        distUiNE[inx(isaveUiE[inx(igrid, tgrid)], k)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)];
        distUiNE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)];
        distUiEtoUiNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)] + expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)];
        
            
        // receive pLTpar, receive business idea, does not receive job, receive phipar
        #if NOPOLICY == 1
        expprob = mprod[tgrid][k]*(phipar)*(1.0 - pLTpar)*(piE(searchUiE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUiE_W[inx(igrid, tgrid)]));
        distUiNE[inx(isaveUiE[inx(igrid, tgrid)], k)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_0 < (valueUiNE[inx(isaveUiE[inx(igrid, tgrid)], k)])))?(1.0):(0.0));
        distUiNE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_1 < (valueUiNE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
        distUiEtoUiNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_0 < (valueUiNE[inx(isaveUiE[inx(igrid, tgrid)], k)])))?(1.0):(0.0)) + expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_1 < (valueUiNE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
        #endif
        
        #if SEA == 1
        expprob = mprod[tgrid][k]*(phipar)*(1.0 - pLTpar)*(piE(searchUiE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUiE_W[inx(igrid, tgrid)]));
        distUiNE[inx(isaveUiE[inx(igrid, tgrid)], k)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNEi_0 < (valueUiNE[inx(isaveUiE[inx(igrid, tgrid)], k)])))?(1.0):(0.0));
        distUiNE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNEi_1 < (valueUiNE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
        distUiEtoUiNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNEi_0 < (valueUiNE[inx(isaveUiE[inx(igrid, tgrid)], k)])))?(1.0):(0.0)) + expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNEi_1 < (valueUiNE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
        #endif


        // TRANSITION TO UNEMPLOYED INSURED -> EXCLUDED
        // receive pLTpar, does not receive business idea, does not receive job, receive phipar
        expprob = mprod[tgrid][k]*(1.0 - phipar)*(1.0 - pLTpar)*(1.0 - piE(searchUiE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUiE_W[inx(igrid, tgrid)]));
        distUiE[inx(isaveUiE[inx(igrid, tgrid)], k)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)];
        distUiE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)];
        
            
        // receive pLTpar, receive business idea, does not receive job, receive phipar
        #if NOPOLICY == 1
        expprob = mprod[tgrid][k]*(1.0 - phipar)*(1.0 - pLTpar)*(piE(searchUiE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUiE_W[inx(igrid, tgrid)]));
        distUiE[inx(isaveUiE[inx(igrid, tgrid)], k)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JE_0 < (valueUiE[inx(isaveUiE[inx(igrid, tgrid)], k)])))?(1.0):(0.0));
        distUiE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JE_1 < (valueUiE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
        #endif
        
        #if SEA == 1
        expprob = mprod[tgrid][k]*(1.0 - phipar)*(1.0 - pLTpar)*(piE(searchUiE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUiE_W[inx(igrid, tgrid)]));
        distUiE[inx(isaveUiE[inx(igrid, tgrid)], k)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JEi_0 < (valueUiE[inx(isaveUiE[inx(igrid, tgrid)], k)])))?(1.0):(0.0));
        distUiE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JEi_1 < (valueUiE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
        #endif
        
        
        /*** UNEMPLOYED UNINSURED EXCLUDED ***/
        // TRANSITION TO UNEMPLOYED UNINSURED -> NOT-EXCLUDED
        // does not receive business idea, does not receive job, receive phipar
        expprob = mprod[tgrid][k]*(phipar)*(1.0 - piE(searchUnE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUnE_W[inx(igrid, tgrid)]));
        distUnNE[inx(isaveUnE[inx(igrid, tgrid)], k)] += expprob*(1.0 - saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)];
        distUnNE[inx(min((maxgrid-1),(isaveUnE[inx(igrid, tgrid)]+1)), k)] += expprob*(saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)];
        distUnEtoUnNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)] + expprob*(saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)];
        
        //  receive business idea, does not receive job, receive phipar
        expprob = mprod[tgrid][k]*(phipar)*(piE(searchUnE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUnE_W[inx(igrid, tgrid)]));
        distUnNE[inx(isaveUnE[inx(igrid, tgrid)], k)] += expprob*(1.0 - saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)]*(((tempUnE_JNE_0 < (valueUnNE[inx(isaveUnE[inx(igrid, tgrid)], k)])))?(1.0):(0.0));
        distUnNE[inx(min((maxgrid-1),(isaveUnE[inx(igrid, tgrid)]+1)), k)] += expprob*(saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)]*(((tempUnE_JNE_1 < (valueUnNE[inx(min((maxgrid-1),(isaveUnE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
        distUnEtoUnNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)]*(((tempUnE_JNE_0 < (valueUnNE[inx(isaveUnE[inx(igrid, tgrid)], k)])))?(1.0):(0.0)) + expprob*(saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)]*(((tempUnE_JNE_1 < (valueUnNE[inx(min((maxgrid-1),(isaveUnE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
        
            
            
        // TRANSITION TO UNEMPLOYED UNINSURED -> EXCLUDED
        // does not receive business idea, does not receive job, receive phipar
        expprob = mprod[tgrid][k]*(1.0 - phipar)*(1.0 - piE(searchUnE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUnE_W[inx(igrid, tgrid)]));
        distUnE[inx(isaveUnE[inx(igrid, tgrid)], k)] += expprob*(1.0 - saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)];
        distUnE[inx(min((maxgrid-1),(isaveUnE[inx(igrid, tgrid)]+1)), k)] += expprob*(saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)];
            
        // receive business idea, does not receive job, receive phipar
        expprob = mprod[tgrid][k]*(1.0 - phipar)*(piE(searchUnE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUnE_W[inx(igrid, tgrid)]));
        distUnE[inx(isaveUnE[inx(igrid, tgrid)], k)] += expprob*(1.0 - saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)]*(((tempUnE_JE_0 < (valueUnE[inx(isaveUnE[inx(igrid, tgrid)], k)])))?(1.0):(0.0));
        distUnE[inx(min((maxgrid-1),(isaveUnE[inx(igrid, tgrid)]+1)), k)] += expprob*(saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)]*(((tempUnE_JE_1 < (valueUnE[inx(min((maxgrid-1),(isaveUnE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
            
            
        /*** UNEMPLOYED INSURED NOT EXCLUDED ***/
        // TRANSITION TO UNEMPLOYED NOT-INSURED -> NOT EXCLUDED
        // receive pLTpar, does not receive business idea, does not receive job,
        expprob = mprod[tgrid][k]*(pLTpar)*(1.0 - piE(searchUiNE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUiNE_W[inx(igrid, tgrid)]));
        distUnNE[inx(isaveUiNE[inx(igrid, tgrid)], k)] += expprob*(1.0 - saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)];
        distUnNE[inx(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k)] += expprob*(saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)];
        distUiNEtoUnNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)] + expprob*(saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)];
        
        // receive pLTpar, receive business idea, does not receive job,
        expprob = mprod[tgrid][k]*(pLTpar)*(piE(searchUiNE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUiNE_W[inx(igrid, tgrid)]));
        distUnNE[inx(isaveUiNE[inx(igrid, tgrid)], k)] += expprob*(1.0 - saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNE_0 < (valueUnNE[inx(isaveUiNE[inx(igrid, tgrid)], k)])))?(1.0):(0.0));
        distUnNE[inx(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k)] += expprob*(saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNE_1 < (valueUnNE[inx(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
        distUiNEtoUnNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNE_0 < (valueUnNE[inx(isaveUiNE[inx(igrid, tgrid)], k)])))?(1.0):(0.0)) + expprob*(saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNE_1 < (valueUnNE[inx(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));


        // TRANSITION TO UNEMPLOYED INSURED -> NOT-EXCLUDED
        // receive pLTpar, does not receive business idea, does not receive job,
        expprob = mprod[tgrid][k]*(1.0 - pLTpar)*(1.0 - piE(searchUiNE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUiNE_W[inx(igrid, tgrid)]));
        distUiNE[inx(isaveUiNE[inx(igrid, tgrid)], k)] += expprob*(1.0 - saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)];
        distUiNE[inx(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k)] += expprob*(saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)];
            
        // receive pLTpar, receive business idea, does not receive job,
        #if NOPOLICY == 1
        expprob = mprod[tgrid][k]*(1.0 - pLTpar)*(piE(searchUiNE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUiNE_W[inx(igrid, tgrid)]));
        distUiNE[inx(isaveUiNE[inx(igrid, tgrid)], k)] += expprob*(1.0 - saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNE_0 < (valueUiNE[inx(isaveUiNE[inx(igrid, tgrid)], k)])))?(1.0):(0.0));
        distUiNE[inx(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k)] += expprob*(saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNE_1 < (valueUiNE[inx(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
        #endif
        
        #if SEA == 1
        expprob = mprod[tgrid][k]*(1.0 - pLTpar)*(piE(searchUiNE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUiNE_W[inx(igrid, tgrid)]));
        distUiNE[inx(isaveUiNE[inx(igrid, tgrid)], k)] += expprob*(1.0 - saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNEi_0 < (valueUiNE[inx(isaveUiNE[inx(igrid, tgrid)], k)])))?(1.0):(0.0));
        distUiNE[inx(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k)] += expprob*(saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNEi_1 < (valueUiNE[inx(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
        #endif
        
        
            
        /*** UNEMPLOYED UNINSURED NOT EXCLUDED ***/
        // TRANSITION TO UNEMPLOYED INSURED -> NOT-EXCLUDED
        // does not receive business idea, does not receive job,
        expprob = mprod[tgrid][k]*(1.0 - piE(searchUnNE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUnNE_W[inx(igrid, tgrid)]));
        distUnNE[inx(isaveUnNE[inx(igrid, tgrid)], k)] += expprob*(1.0 - saveresUnNE[inx(igrid, tgrid)])*distUnNEold[inx(igrid, tgrid)];
        distUnNE[inx(min((maxgrid-1),(isaveUnNE[inx(igrid, tgrid)]+1)), k)] += expprob*(saveresUnNE[inx(igrid, tgrid)])*distUnNEold[inx(igrid, tgrid)];
            
        // receive business idea, does not receive job,
        expprob = mprod[tgrid][k]*(piE(searchUnNE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUnNE_W[inx(igrid, tgrid)]));
        distUnNE[inx(isaveUnNE[inx(igrid, tgrid)], k)] += expprob*(1.0 - saveresUnNE[inx(igrid, tgrid)])*distUnNEold[inx(igrid, tgrid)]*(((tempUnNE_JNE_0 < (valueUnNE[inx(isaveUnNE[inx(igrid, tgrid)], k)])))?(1.0):(0.0));
        distUnNE[inx(min((maxgrid-1),(isaveUnNE[inx(igrid, tgrid)]+1)), k)] += expprob*(saveresUnNE[inx(igrid, tgrid)])*distUnNEold[inx(igrid, tgrid)]*(((tempUnNE_JNE_1 < (valueUnNE[inx(min((maxgrid-1),(isaveUnNE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
        
        

        // receive a job offer --> y realization always choose to be worker, instead of unemployed
        for(y=0; y < maxyprod; y++) {
        
            /*** UNEMPLOYED INSURED EXCLUDED ***/
            
            // TRANSITION TO WORKER EXCLUDED //
        
            // not receive phipar, not fall in uninsured unemployed, receive a job, does not receive an entrepreneurial offer //
            expprob = invmyprod[y]*mprod[tgrid][k]*(1.0 - phipar)*(1.0 - pLTpar)*(1.0 - piE(searchUiE_J[inx(igrid, tgrid)]))*(piW( searchUiE_W[inx(igrid, tgrid)]));
            distWWE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)];
            distWWE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)];
            distUiEtoWWE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)] + expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)];
            
            
            // not receive phipar, receive a job, receive an entrepreneurial offer //
            #if NOPOLICY == 1
            expprob = invmyprod[y]*mprod[tgrid][k]*(1.0 - phipar)*(1.0 - pLTpar)*(piE(searchUiE_J[inx(igrid, tgrid)]))*(piW( searchUiE_W[inx(igrid, tgrid)]));
            distWWE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JE_0 <= (valueWWE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0));
            distWWE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JE_1 <= (valueWWE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
            distUiEtoWWE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JE_0 <= (valueWWE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0)) + expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JE_1 <= (valueWWE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
            #endif
            
            #if SEA == 1
            expprob = invmyprod[y]*mprod[tgrid][k]*(1.0 - phipar)*(1.0 - pLTpar)*(piE(searchUiE_J[inx(igrid, tgrid)]))*(piW( searchUiE_W[inx(igrid, tgrid)]));
            distWWE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JEi_0 <= (valueWWE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0));
            distWWE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JEi_1 <= (valueWWE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
            distUiEtoWWE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JEi_0 <= (valueWWE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0)) + expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JEi_1 <= (valueWWE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
            #endif
            
            // not receive phipar, fall in uninsured unemployed, receive a job, does not receive an entrepreneurial offer //
            expprob = invmyprod[y]*mprod[tgrid][k]*(1.0 - phipar)*(pLTpar)*(1.0 - piE(searchUiE_J[inx(igrid, tgrid)]))*(piW( searchUiE_W[inx(igrid, tgrid)]));
            distWWE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)];
            distWWE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)];
            distUiEtoWWE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)] + expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)];
            
            // not receive phipar, fall in uninsured unemployed, receive a job, receive an entrepreneurial offer //
            expprob = invmyprod[y]*mprod[tgrid][k]*(1.0 - phipar)*(pLTpar)*(piE(searchUiE_J[inx(igrid, tgrid)]))*(piW( searchUiE_W[inx(igrid, tgrid)]));
            distWWE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JE_0 <= (valueWWE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0));
            distWWE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JE_1 <= (valueWWE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
            distUiEtoWWE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JE_0 <= (valueWWE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0)) + expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JE_1 <= (valueWWE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
            
            
            
            // TRANSITION TO WORKER NOT-EXCLUDED //
            
            // receive phipar, receive a job, not fall in uninsured unemployed, does not receive an entrepreneurial offer //
            expprob = invmyprod[y]*mprod[tgrid][k]*(phipar)*(1.0 - pLTpar)*(1.0 - piE(searchUiE_J[inx(igrid, tgrid)]))*(piW( searchUiE_W[inx(igrid, tgrid)]));
            distWWNE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)];
            distWWNE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)];
            distUiEtoWWNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)] + expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)];
            
            
            // not receive phipar, receive a job, receive an entrepreneurial offer //
            #if NOPOLICY == 1
            expprob = invmyprod[y]*mprod[tgrid][k]*(phipar)*(1.0 - pLTpar)*(piE(searchUiE_J[inx(igrid, tgrid)]))*(piW( searchUiE_W[inx(igrid, tgrid)]));
            distWWNE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_0 <= (valueWWNE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0));
            distWWNE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_1 <= (valueWWNE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
            distUiEtoWWNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_0 <= (valueWWNE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0)) + expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_1 <= (valueWWNE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
            #endif
            
            #if SEA == 1
            expprob = invmyprod[y]*mprod[tgrid][k]*(phipar)*(1.0 - pLTpar)*(piE(searchUiE_J[inx(igrid, tgrid)]))*(piW( searchUiE_W[inx(igrid, tgrid)]));
            distWWNE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNEi_0 <= (valueWWNE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0));
            distWWNE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNEi_1 <= (valueWWNE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
            distUiEtoWWNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNEi_0 <= (valueWWNE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0)) + expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNEi_1 <= (valueWWNE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
            #endif
            
            // receive phipar, receive a job, not fall in uninsured unemployed, does not receive an entrepreneurial offer //
            expprob = invmyprod[y]*mprod[tgrid][k]*(phipar)*(pLTpar)*(1.0 - piE(searchUiE_J[inx(igrid, tgrid)]))*(piW( searchUiE_W[inx(igrid, tgrid)]));
            distWWNE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)];
            distWWNE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)];
            distUiEtoWWNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)] + expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)];
            
            
            // not receive phipar, receive a job, fall in uninsurance, receive an entrepreneurial offer //
            expprob = invmyprod[y]*mprod[tgrid][k]*(phipar)*(pLTpar)*(piE(searchUiE_J[inx(igrid, tgrid)]))*(piW( searchUiE_W[inx(igrid, tgrid)]));
            distWWNE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_0 <= (valueWWNE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0));
            distWWNE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_1 <= (valueWWNE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
            distUiEtoWWNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_0 <= (valueWWNE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0)) + expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_1 <= (valueWWNE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
            
            
            
            /*** UNEMPLOYED UNINSURED EXCLUDED ***/
            // TRANSITION TO WORKER EXCLUDED //
        
            // not receive phipar, not fall in uninsured unemployed, receive a job, does not receive an entrepreneurial offer //
            expprob = invmyprod[y]*mprod[tgrid][k]*(1.0 - phipar)*(1.0 - piE(searchUnE_J[inx(igrid, tgrid)]))*(piW( searchUnE_W[inx(igrid, tgrid)]));
            distWWE[inxW(isaveUnE[inx(igrid, tgrid)], k, y)] += expprob*(1.0 - saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)];
            distWWE[inxW(min((maxgrid-1),(isaveUnE[inx(igrid, tgrid)]+1)), k, y)] += expprob*(saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)];
            distUnEtoWWE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)] + expprob*(saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)];
            
            
            // not receive phipar, receive a job, receive an entrepreneurial offer //
            expprob = invmyprod[y]*mprod[tgrid][k]*(1.0 - phipar)*(piE(searchUnE_J[inx(igrid, tgrid)]))*(piW( searchUnE_W[inx(igrid, tgrid)]));
            distWWE[inxW(isaveUnE[inx(igrid, tgrid)], k, y)] += expprob*(1.0 - saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)]*(((tempUnE_JE_0 <= (valueWWE[inxW(isaveUnE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0));
            distWWE[inxW(min((maxgrid-1),(isaveUnE[inx(igrid, tgrid)]+1)), k, y)] += expprob*(saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)]*(((tempUnE_JE_1 <= (valueWWE[inxW(min((maxgrid-1),(isaveUnE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
            distUnEtoWWE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)]*(((tempUnE_JE_0 <= (valueWWE[inxW(isaveUnE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0)) + expprob*(saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)]*(((tempUnE_JE_1 <= (valueWWE[inxW(min((maxgrid-1),(isaveUnE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
            
            
            // TRANSITION TO WORKER NOT-EXCLUDED //
            
            // receive phipar, receive a job, not fall in uninsured unemployed, does not receive an entrepreneurial offer //
            expprob = invmyprod[y]*mprod[tgrid][k]*(phipar)*(1.0 - piE(searchUnE_J[inx(igrid, tgrid)]))*(piW( searchUnE_W[inx(igrid, tgrid)]));
            distWWNE[inxW(isaveUnE[inx(igrid, tgrid)], k, y)] += expprob*(1.0 - saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)];
            distWWNE[inxW(min((maxgrid-1),(isaveUnE[inx(igrid, tgrid)]+1)), k, y)] += expprob*(saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)];
            distUnEtoWWNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)] + expprob*(saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)];
            
            
            // not receive phipar, receive a job, receive an entrepreneurial offer //
            expprob = invmyprod[y]*mprod[tgrid][k]*(phipar)*(piE(searchUnE_J[inx(igrid, tgrid)]))*(piW( searchUnE_W[inx(igrid, tgrid)]));
            distWWNE[inxW(isaveUnE[inx(igrid, tgrid)], k, y)] += expprob*(1.0 - saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)]*(((tempUnE_JNE_0 <= (valueWWNE[inxW(isaveUnE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0));
            distWWNE[inxW(min((maxgrid-1),(isaveUnE[inx(igrid, tgrid)]+1)), k, y)] += expprob*(saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)]*(((tempUnE_JNE_1 <= (valueWWNE[inxW(min((maxgrid-1),(isaveUnE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
            distUnEtoWWNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)]*(((tempUnE_JNE_0 <= (valueWWNE[inxW(isaveUnE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0)) + expprob*(saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)]*(((tempUnE_JNE_1 <= (valueWWNE[inxW(min((maxgrid-1),(isaveUnE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));

            
            /*** UNEMPLOYED INSURED NOT EXCLUDED ***/
            // TRANSITION TO WORKER NOT-EXCLUDED //
            
            // receive a job, not fall in uninsured unemployed, does not receive an entrepreneurial offer //
            expprob = invmyprod[y]*mprod[tgrid][k]*(1.0 - pLTpar)*(1.0 - piE(searchUiNE_J[inx(igrid, tgrid)]))*(piW( searchUiNE_W[inx(igrid, tgrid)]));
            distWWNE[inxW(isaveUiNE[inx(igrid, tgrid)], k, y)] += expprob*(1.0 - saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)];
            distWWNE[inxW(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k, y)] += expprob*(saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)];
            distUiNEtoWWNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)] + expprob*(saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)];
            
            // receive a job, receive an entrepreneurial offer //
            #if NOPOLICY == 1
            expprob = invmyprod[y]*mprod[tgrid][k]*(1.0 - pLTpar)*(piE(searchUiNE_J[inx(igrid, tgrid)]))*(piW( searchUiNE_W[inx(igrid, tgrid)]));
            distWWNE[inxW(isaveUiNE[inx(igrid, tgrid)], k, y)] += expprob*(1.0 - saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNE_0 <= (valueWWNE[inxW(isaveUiNE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0));
            distWWNE[inxW(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k, y)] += expprob*(saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNE_1 <= (valueWWNE[inxW(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
            distUiNEtoWWNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNE_0 <= (valueWWNE[inxW(isaveUiNE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0)) + expprob*(saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNE_1 <= (valueWWNE[inxW(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
            #endif
            
            #if SEA == 1
            expprob = invmyprod[y]*mprod[tgrid][k]*(1.0 - pLTpar)*(piE(searchUiNE_J[inx(igrid, tgrid)]))*(piW( searchUiNE_W[inx(igrid, tgrid)]));
            distWWNE[inxW(isaveUiNE[inx(igrid, tgrid)], k, y)] += expprob*(1.0 - saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNEi_0 <= (valueWWNE[inxW(isaveUiNE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0));
            distWWNE[inxW(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k, y)] += expprob*(saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNEi_1 <= (valueWWNE[inxW(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
            distUiNEtoWWNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNEi_0 <= (valueWWNE[inxW(isaveUiNE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0)) + expprob*(saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNEi_1 <= (valueWWNE[inxW(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
            #endif
            
            // receive a job, fall in uninsured unemployed, does not receive an entrepreneurial offer //
            expprob = invmyprod[y]*mprod[tgrid][k]*(pLTpar)*(1.0 - piE(searchUiNE_J[inx(igrid, tgrid)]))*(piW( searchUiNE_W[inx(igrid, tgrid)]));
            distWWNE[inxW(isaveUiNE[inx(igrid, tgrid)], k, y)] += expprob*(1.0 - saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)];
            distWWNE[inxW(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k, y)] += expprob*(saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)];
            distUiNEtoWWNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)] + expprob*(saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)];
            
            // receive a job, fall in uninsured unemployed, receive an entrepreneurial offer //
            expprob = invmyprod[y]*mprod[tgrid][k]*(pLTpar)*(piE(searchUiNE_J[inx(igrid, tgrid)]))*(piW( searchUiNE_W[inx(igrid, tgrid)]));
            distWWNE[inxW(isaveUiNE[inx(igrid, tgrid)], k, y)] += expprob*(1.0 - saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNE_0 <= (valueWWNE[inxW(isaveUiNE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0));
            distWWNE[inxW(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k, y)] += expprob*(saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNE_1 <= (valueWWNE[inxW(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
            distUiNEtoWWNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNE_0 <= (valueWWNE[inxW(isaveUiNE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0)) + expprob*(saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNE_1 <= (valueWWNE[inxW(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
            
            
            
            /*** UNEMPLOYED UNINSURED NOT EXCLUDED ***/
            // TRANSITION TO WORKER NOT-EXCLUDED //
            
            // receive a job, not fall in uninsured unemployed, does not receive an entrepreneurial offer //
            expprob = invmyprod[y]*mprod[tgrid][k]*(1.0 - piE(searchUnNE_J[inx(igrid, tgrid)]))*(piW( searchUnNE_W[inx(igrid, tgrid)]));
            distWWNE[inxW(isaveUnNE[inx(igrid, tgrid)], k, y)] += expprob*(1.0 - saveresUnNE[inx(igrid, tgrid)])*distUnNEold[inx(igrid, tgrid)];
            distWWNE[inxW(min((maxgrid-1),(isaveUnNE[inx(igrid, tgrid)]+1)), k, y)] += expprob*(saveresUnNE[inx(igrid, tgrid)])*distUnNEold[inx(igrid, tgrid)];
            distUnNEtoWWNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUnNE[inx(igrid, tgrid)])*distUnNEold[inx(igrid, tgrid)] + expprob*(saveresUnNE[inx(igrid, tgrid)])*distUnNEold[inx(igrid, tgrid)];
            
            // receive a job, receive an entrepreneurial offer //
            expprob = invmyprod[y]*mprod[tgrid][k]*(piE(searchUnNE_J[inx(igrid, tgrid)]))*(piW( searchUnNE_W[inx(igrid, tgrid)]));
            distWWNE[inxW(isaveUnNE[inx(igrid, tgrid)], k, y)] += expprob*(1.0 - saveresUnNE[inx(igrid, tgrid)])*distUnNEold[inx(igrid, tgrid)]*(((tempUnNE_JNE_0 <= (valueWWNE[inxW(isaveUnNE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0));
            distWWNE[inxW(min((maxgrid-1),(isaveUnNE[inx(igrid, tgrid)]+1)), k, y)] += expprob*(saveresUnNE[inx(igrid, tgrid)])*distUnNEold[inx(igrid, tgrid)]*(((tempUnNE_JNE_1 <= (valueWWNE[inxW(min((maxgrid-1),(isaveUnNE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
            distUnNEtoWWNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUnNE[inx(igrid, tgrid)])*distUnNEold[inx(igrid, tgrid)]*(((tempUnNE_JNE_0 <= (valueWWNE[inxW(isaveUnNE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0)) + expprob*(saveresUnNE[inx(igrid, tgrid)])*distUnNEold[inx(igrid, tgrid)]*(((tempUnNE_JNE_1 <= (valueWWNE[inxW(min((maxgrid-1),(isaveUnNE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
            
            
            
        }
        
        
        // TRANSITION TO ENTREPRENEUR
        for(e=0; e< maxfirmtype; e++) {
            
            /*** UNEMPLOYED INSURED EXCLUDED ***/
            // (WITHOUT JOB OPPORTUNITY) //
            
            #if NOPOLICY == 1
            // TRANSITION TO ENTREPRENEUR EXCLUDED
            // not receive phipar, not fall in uninsurance, not receive job opportunity
            expprob = mzinv[e]*mprod[tgrid][k]*(1.0 - phipar)*(1.0 - pLTpar)*(piE(searchUiE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUiE_W[inx(igrid, tgrid)]));
            distJE[inxE(isaveUiE[inx(igrid, tgrid)], k, e)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JE_0 >= (valueUiE[inx(isaveUiE[inx(igrid, tgrid)], k)])))?(1.0):(0.0));
            distJE[inxE(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, e)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JE_1 >= (valueUiE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
            distUiEtoJE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JE_0 >= (valueUiE[inx(isaveUiE[inx(igrid, tgrid)], k)])))?(1.0):(0.0)) + expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JE_1 >= (valueUiE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
            
            
            // TRANSITION TO ENTREPRENEUR NOT-EXCLUDED
            // receive phipar, not fall in uninsurance, not receive job opportunity
            expprob = mzinv[e]*mprod[tgrid][k]*(phipar)*(1.0 - pLTpar)*(piE(searchUiE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUiE_W[inx(igrid, tgrid)]));
            distJNE[inxE(isaveUiE[inx(igrid, tgrid)], k, e)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_0 >= (valueUiNE[inx(isaveUiE[inx(igrid, tgrid)], k)])))?(1.0):(0.0));
            distJNE[inxE(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, e)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_1 >= (valueUiNE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
            distUiEtoJNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_0 >= (valueUiNE[inx(isaveUiE[inx(igrid, tgrid)], k)])))?(1.0):(0.0)) + expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_1 >= (valueUiNE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));

            // not receive phipar, fall in uninsurance, not receive job opportunity
            expprob = mzinv[e]*mprod[tgrid][k]*(1.0 - phipar)*(pLTpar)*(piE(searchUiE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUiE_W[inx(igrid, tgrid)]));
            distJE[inxE(isaveUiE[inx(igrid, tgrid)], k, e)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JE_0 >= (valueUnE[inx(isaveUiE[inx(igrid, tgrid)], k)])))?(1.0):(0.0));
            distJE[inxE(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, e)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JE_1 >= (valueUnE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
            distUnEtoJE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JE_0 >= (valueUnE[inx(isaveUiE[inx(igrid, tgrid)], k)])))?(1.0):(0.0)) + expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JE_1 >= (valueUnE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
            
            
            // receive phipar, fall in uninsurance, not receive job opportunity
            expprob = mzinv[e]*mprod[tgrid][k]*(phipar)*(pLTpar)*(piE(searchUiE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUiE_W[inx(igrid, tgrid)]));
            distJNE[inxE(isaveUiE[inx(igrid, tgrid)], k, e)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_0 >= (valueUnNE[inx(isaveUiE[inx(igrid, tgrid)], k)])))?(1.0):(0.0));
            distJNE[inxE(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, e)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_1 >= (valueUnNE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
            distUnEtoJNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_0 >= (valueUnNE[inx(isaveUiE[inx(igrid, tgrid)], k)])))?(1.0):(0.0)) + expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_1 >= (valueUnNE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
            #endif
            
            
            #if SEA == 1
            // TRANSITION TO ENTREPRENEUR EXCLUDED
            // not receive phipar, not fall in uninsurance, not receive job opportunity
            expprob = mzinv[e]*mprod[tgrid][k]*(1.0 - phipar)*(1.0 - pLTpar)*(piE(searchUiE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUiE_W[inx(igrid, tgrid)]));
            distJEi[inxE(isaveUiE[inx(igrid, tgrid)], k, e)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JEi_0 >= (valueUiE[inx(isaveUiE[inx(igrid, tgrid)], k)])))?(1.0):(0.0));
            distJEi[inxE(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, e)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JEi_1 >= (valueUiE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
            distUiEtoJE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JEi_0 >= (valueUiE[inx(isaveUiE[inx(igrid, tgrid)], k)])))?(1.0):(0.0)) + expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JEi_1 >= (valueUiE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
            
            if(tempUiE_JEi_0 >= (valueUiE[inx(isaveUiE[inx(igrid, tgrid)], k)]) & tempUiE_JE_0 <= (valueUiE[inx(isaveUiE[inx(igrid, tgrid)], k)])) {
                distUiEtoJE_select[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JEi_0 >= (valueUiE[inx(isaveUiE[inx(igrid, tgrid)], k)])))?(1.0):(0.0));
            }
            if(tempUiE_JEi_1 >= (valueUiE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)]) & tempUiE_JE_1 <= (valueUiE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)])) {
                distUiEtoJE_select[inx(igrid, tgrid)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JEi_1 >= (valueUiE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
            }
            
            // not receive phipar, fall in uninsurance, not receive job opportunity
            expprob = mzinv[e]*mprod[tgrid][k]*(1.0 - phipar)*(pLTpar)*(piE(searchUiE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUiE_W[inx(igrid, tgrid)]));
            distJE[inxE(isaveUiE[inx(igrid, tgrid)], k, e)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JE_0 >= (valueUnE[inx(isaveUiE[inx(igrid, tgrid)], k)])))?(1.0):(0.0));
            distJE[inxE(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, e)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JE_1 >= (valueUnE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
            distUnEtoJE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JE_0 >= (valueUnE[inx(isaveUiE[inx(igrid, tgrid)], k)])))?(1.0):(0.0)) + expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JE_1 >= (valueUnE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
            
            
            // TRANSITION TO ENTREPRENEUR NOT-EXCLUDED
            // receive phipar, not fall in uninsurance, not receive job opportunity
            expprob = mzinv[e]*mprod[tgrid][k]*(phipar)*(1.0 - pLTpar)*(piE(searchUiE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUiE_W[inx(igrid, tgrid)]));
            distJNEi[inxE(isaveUiE[inx(igrid, tgrid)], k, e)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNEi_0 >= (valueUiNE[inx(isaveUiE[inx(igrid, tgrid)], k)])))?(1.0):(0.0));
            distJNEi[inxE(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, e)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNEi_1 >= (valueUiNE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
            distUiEtoJNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNEi_0 >= (valueUiNE[inx(isaveUiE[inx(igrid, tgrid)], k)])))?(1.0):(0.0)) + expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNEi_1 >= (valueUiNE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
            
            if(tempUiE_JNEi_0 >= (valueUiNE[inx(isaveUiE[inx(igrid, tgrid)], k)]) & tempUiE_JNE_0 <= (valueUiNE[inx(isaveUiE[inx(igrid, tgrid)], k)])) {
                distUiEtoJNE_select[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNEi_0 >= (valueUiNE[inx(isaveUiE[inx(igrid, tgrid)], k)])))?(1.0):(0.0));
            }
            if(tempUiE_JNEi_1 >= (valueUiNE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)]) & tempUiE_JNE_1 <= (valueUiNE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)])) {
                distUiEtoJNE_select[inx(igrid, tgrid)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNEi_1 >= (valueUiNE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
            }
            
            // receive phipar, fall in uninsurance, not receive job opportunity
            expprob = mzinv[e]*mprod[tgrid][k]*(phipar)*(pLTpar)*(piE(searchUiE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUiE_W[inx(igrid, tgrid)]));
            distJNE[inxE(isaveUiE[inx(igrid, tgrid)], k, e)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_0 >= (valueUnNE[inx(isaveUiE[inx(igrid, tgrid)], k)])))?(1.0):(0.0));
            distJNE[inxE(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, e)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_1 >= (valueUnNE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
            distUnEtoJNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_0 >= (valueUnNE[inx(isaveUiE[inx(igrid, tgrid)], k)])))?(1.0):(0.0)) + expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_1 >= (valueUnNE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
            #endif
            
            
            /*** UNEMPLOYED UNINSURED EXCLUDED ***/
            // (WITHOUT JOB OPPORTUNITY) //
            
            // TRANSITION TO ENTREPRENEUR EXCLUDED
            // not receive phipar, not fall in uninsurance, not receive job opportunity
            expprob = mzinv[e]*mprod[tgrid][k]*(1.0 - phipar)*(piE(searchUnE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUnE_W[inx(igrid, tgrid)]));
            distJE[inxE(isaveUnE[inx(igrid, tgrid)], k, e)] += expprob*(1.0 - saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)]*(((tempUnE_JE_0 >= (valueUnE[inx(isaveUnE[inx(igrid, tgrid)], k)])))?(1.0):(0.0));
            distJE[inxE(min((maxgrid-1),(isaveUnE[inx(igrid, tgrid)]+1)), k, e)] += expprob*(saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)]*(((tempUnE_JE_1 >= (valueUnE[inx(min((maxgrid-1),(isaveUnE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
            distUnEtoJE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)]*(((tempUnE_JE_0 >= (valueUnE[inx(isaveUnE[inx(igrid, tgrid)], k)])))?(1.0):(0.0)) + expprob*(saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)]*(((tempUnE_JE_1 >= (valueUnE[inx(min((maxgrid-1),(isaveUnE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
            
            
            // TRANSITION TO ENTREPRENEUR NOT-EXCLUDED
            // receive phipar, not fall in uninsurance, not receive job opportunity
            expprob = mzinv[e]*mprod[tgrid][k]*(phipar)*(piE(searchUnE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUnE_W[inx(igrid, tgrid)]));
            distJNE[inxE(isaveUnE[inx(igrid, tgrid)], k, e)] += expprob*(1.0 - saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)]*(((tempUnE_JNE_0 >= (valueUnNE[inx(isaveUnE[inx(igrid, tgrid)], k)])))?(1.0):(0.0));
            distJNE[inxE(min((maxgrid-1),(isaveUnE[inx(igrid, tgrid)]+1)), k, e)] += expprob*(saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)]*(((tempUnE_JNE_1 >= (valueUnNE[inx(min((maxgrid-1),(isaveUnE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
            distUnEtoJNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)]*(((tempUnE_JNE_0 >= (valueUnNE[inx(isaveUnE[inx(igrid, tgrid)], k)])))?(1.0):(0.0)) + expprob*(saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)]*(((tempUnE_JNE_1 >= (valueUnNE[inx(min((maxgrid-1),(isaveUnE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
            
            
            
            /*** UNEMPLOYED INSURED NOT EXCLUDED ***/
            // (WITHOUT JOB OPPORTUNITY) //
            
            // TRANSITION TO ENTREPRENEUR NOT-EXCLUDED
            // not fall in uninsurance, not receive job opportunity
            #if NOPOLICY == 1
            expprob = mzinv[e]*mprod[tgrid][k]*(1.0 - pLTpar)*(piE(searchUiNE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUiNE_W[inx(igrid, tgrid)]));
            distJNE[inxE(isaveUiNE[inx(igrid, tgrid)], k, e)] += expprob*(1.0 - saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNE_0 >= (valueUiNE[inx(isaveUiNE[inx(igrid, tgrid)], k)])))?(1.0):(0.0));
            distJNE[inxE(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k, e)] += expprob*(saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNE_1 >= (valueUiNE[inx(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
            distUiNEtoJNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNE_0 >= (valueUiNE[inx(isaveUiNE[inx(igrid, tgrid)], k)])))?(1.0):(0.0)) + expprob*(saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNE_1 >= (valueUiNE[inx(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
            #endif
            
            #if SEA == 1
            expprob = mzinv[e]*mprod[tgrid][k]*(1.0 - pLTpar)*(piE(searchUiNE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUiNE_W[inx(igrid, tgrid)]));
            distJNEi[inxE(isaveUiNE[inx(igrid, tgrid)], k, e)] += expprob*(1.0 - saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNEi_0 >= (valueUiNE[inx(isaveUiNE[inx(igrid, tgrid)], k)])))?(1.0):(0.0));
            distJNEi[inxE(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k, e)] += expprob*(saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNEi_1 >= (valueUiNE[inx(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
            distUiNEtoJNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNEi_0 >= (valueUiNE[inx(isaveUiNE[inx(igrid, tgrid)], k)])))?(1.0):(0.0)) + expprob*(saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNEi_1 >= (valueUiNE[inx(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
            #endif

            // fall in uninsurance, not receive job opportunity
            expprob = mzinv[e]*mprod[tgrid][k]*(pLTpar)*(piE(searchUiNE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUiNE_W[inx(igrid, tgrid)]));
            distJNE[inxE(isaveUiNE[inx(igrid, tgrid)], k, e)] += expprob*(1.0 - saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNE_0 >= (valueUnNE[inx(isaveUiNE[inx(igrid, tgrid)], k)])))?(1.0):(0.0));
            distJNE[inxE(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k, e)] += expprob*(saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNE_1 >= (valueUnNE[inx(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
            distUiNEtoJNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNE_0 >= (valueUnNE[inx(isaveUiNE[inx(igrid, tgrid)], k)])))?(1.0):(0.0)) + expprob*(saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNE_1 >= (valueUnNE[inx(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
            
            
            
            /*** UNEMPLOYED UNINSURED NOT EXCLUDED ***/
            // (WITHOUT JOB OPPORTUNITY) //
            
            // TRANSITION TO ENTREPRENEUR NOT-EXCLUDED
            // not fall in uninsurance, not receive job opportunity
            expprob = mzinv[e]*mprod[tgrid][k]*(piE(searchUnNE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUnNE_W[inx(igrid, tgrid)]));
            distJNE[inxE(isaveUnNE[inx(igrid, tgrid)], k, e)] += expprob*(1.0 - saveresUnNE[inx(igrid, tgrid)])*distUnNEold[inx(igrid, tgrid)]*(((tempUnNE_JNE_0 >= (valueUnNE[inx(isaveUnNE[inx(igrid, tgrid)], k)])))?(1.0):(0.0));
            distJNE[inxE(min((maxgrid-1),(isaveUnNE[inx(igrid, tgrid)]+1)), k, e)] += expprob*(saveresUnNE[inx(igrid, tgrid)])*distUnNEold[inx(igrid, tgrid)]*(((tempUnNE_JNE_1 >= (valueUnNE[inx(min((maxgrid-1),(isaveUnNE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
            distUnNEtoJNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUnNE[inx(igrid, tgrid)])*distUnNEold[inx(igrid, tgrid)]*(((tempUnNE_JNE_0 >= (valueUnNE[inx(isaveUnNE[inx(igrid, tgrid)], k)])))?(1.0):(0.0)) + expprob*(saveresUnNE[inx(igrid, tgrid)])*distUnNEold[inx(igrid, tgrid)]*(((tempUnNE_JNE_1 >= (valueUnNE[inx(min((maxgrid-1),(isaveUnNE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
            
            
            
            // (WITH JOB OPPORTUNITY)
            for(y = 0; y < maxyprod; y++) {
            
                /*** UNEMPLOYED INSURED EXCLUDED ***/
                
                // NECESSITY SHARE //
                // not receive phipar, fall in uninsurance, not receive job opportunity
                expprob = invmyprod[y]*mzinv[e]*mprod[tgrid][k]*(1.0 - phipar)*(pLTpar)*(piE(searchUiE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUiE_W[inx(igrid, tgrid)]));
                if(tempUiE_JE_0 >= valueUnE[inx(isaveUiE[inx(igrid, tgrid)], k)] & valueWWE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)] >= tempUiE_JE_0)
                {
                    necessityUiE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JE_0 >= (valueUnE[inx(isaveUiE[inx(igrid, tgrid)], k)])))?(1.0):(0.0));
                }
                if(tempUiE_JE_1 >= valueUnE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)] & tempUiE_JE_1 <= valueWWE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)])
                {
                    necessityUiE[inx(igrid, tgrid)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JE_1 >= (valueUnE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
                }
                
                // receive phipar, fall in uninsurance, not receive job opportunity
                expprob = invmyprod[y]*mzinv[e]*mprod[tgrid][k]*(phipar)*(pLTpar)*(piE(searchUiE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUiE_W[inx(igrid, tgrid)]));
                if(tempUiE_JNE_0 >= valueUnNE[inx(isaveUiE[inx(igrid, tgrid)], k)] & valueWWNE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)] >= tempUiE_JNE_0)
                {
                    necessityUiE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_0 >= (valueUnNE[inx(isaveUiE[inx(igrid, tgrid)], k)])))?(1.0):(0.0));
                }
                if(tempUiE_JNE_1 >= valueUnNE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)] & tempUiE_JNE_1 <= valueWWNE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)])
                {
                    necessityUiE[inx(igrid, tgrid)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_1 >= (valueUnNE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
                }
                
            
            
                #if NOPOLICY == 1
                // not receive phipar, not fall in uninsurance, not receive job opportunity
                expprob = invmyprod[y]*mzinv[e]*mprod[tgrid][k]*(1.0 - phipar)*(1.0 - pLTpar)*(piE(searchUiE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUiE_W[inx(igrid, tgrid)]));
                if(tempUiE_JE_0 >= valueUiE[inx(isaveUiE[inx(igrid, tgrid)], k)] & valueWWE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)] >= tempUiE_JE_0)
                {
                    necessityUiE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JE_0 >= (valueUiE[inx(isaveUiE[inx(igrid, tgrid)], k)])))?(1.0):(0.0));
                }
                if(tempUiE_JE_1 >= valueUiE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)] & tempUiE_JE_1 <= valueWWE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)])
                {
                    necessityUiE[inx(igrid, tgrid)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JE_1 >= (valueUiE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
                }
                
                // receive phipar, not fall in uninsurance, not receive job opportunity
                expprob = invmyprod[y]*mzinv[e]*mprod[tgrid][k]*(phipar)*(1.0 - pLTpar)*(piE(searchUiE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUiE_W[inx(igrid, tgrid)]));
                if(tempUiE_JNE_0 >= valueUiNE[inx(isaveUiE[inx(igrid, tgrid)], k)] & valueWWNE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)] >= tempUiE_JNE_0)
                {
                    necessityUiE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_0 >= (valueUiNE[inx(isaveUiE[inx(igrid, tgrid)], k)])))?(1.0):(0.0));
                }
                if(tempUiE_JNE_1 >= valueUiNE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)] & tempUiE_JNE_1 <= valueWWNE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)])
                {
                    necessityUiE[inx(igrid, tgrid)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_1 >= (valueUiNE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
                }
                #endif
                
                

                #if SEA == 1
                // not receive phipar, not fall in uninsurance, not receive job opportunity
                expprob = invmyprod[y]*mzinv[e]*mprod[tgrid][k]*(1.0 - phipar)*(1.0 - pLTpar)*(piE(searchUiE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUiE_W[inx(igrid, tgrid)]));
                if((tempUiE_JEi_0*(1.0-alt_def_necessity_SEA) + alt_def_necessity_SEA*tempUiE_JE_0) >= valueUiE[inx(isaveUiE[inx(igrid, tgrid)], k)] & valueWWE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)] >= (tempUiE_JEi_0*(1.0-alt_def_necessity_SEA) + alt_def_necessity_SEA*tempUiE_JE_0))
                {
                    necessityUiE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*((((tempUiE_JEi_0*(1.0-alt_def_necessity_SEA) + alt_def_necessity_SEA*tempUiE_JE_0) >= (valueUiE[inx(isaveUiE[inx(igrid, tgrid)], k)])))?(1.0):(0.0));
                }
                if((tempUiE_JEi_1*(1.0-alt_def_necessity_SEA) + alt_def_necessity_SEA*tempUiE_JE_1) >= valueUiE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)] & (tempUiE_JEi_1*(1.0-alt_def_necessity_SEA) + alt_def_necessity_SEA*tempUiE_JE_1) <= valueWWE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)])
                {
                    necessityUiE[inx(igrid, tgrid)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*((((tempUiE_JEi_1*(1.0-alt_def_necessity_SEA) + alt_def_necessity_SEA*tempUiE_JE_1) >= (valueUiE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
                }
                
                // receive phipar, not fall in uninsurance, not receive job opportunity
                expprob = invmyprod[y]*mzinv[e]*mprod[tgrid][k]*(phipar)*(1.0 - pLTpar)*(piE(searchUiE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUiE_W[inx(igrid, tgrid)]));
                if((tempUiE_JNEi_0*(1.0-alt_def_necessity_SEA) + alt_def_necessity_SEA*tempUiE_JNE_0) >= valueUiNE[inx(isaveUiE[inx(igrid, tgrid)], k)] & valueWWNE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)] >= (tempUiE_JNEi_0*(1.0-alt_def_necessity_SEA) + alt_def_necessity_SEA*tempUiE_JNE_0))
                {
                    necessityUiE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*((((tempUiE_JNEi_0*(1.0-alt_def_necessity_SEA) + alt_def_necessity_SEA*tempUiE_JNE_0) >= (valueUiNE[inx(isaveUiE[inx(igrid, tgrid)], k)])))?(1.0):(0.0));
                }
                if((tempUiE_JNEi_1*(1.0-alt_def_necessity_SEA) + alt_def_necessity_SEA*tempUiE_JNE_1) >= valueUiNE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)] & (tempUiE_JNEi_1*(1.0-alt_def_necessity_SEA) + alt_def_necessity_SEA*tempUiE_JNE_1) <= valueWWNE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)])
                {
                    necessityUiE[inx(igrid, tgrid)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*((((tempUiE_JNEi_1*(1.0-alt_def_necessity_SEA) + alt_def_necessity_SEA*tempUiE_JNE_1) >= (valueUiNE[inx(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
                }
                #endif
   
                
                
                
                #if NOPOLICY == 1
                // TRANSITION TO ENTREPRENEUR EXCLUDED
                
                // not receive phipar, not fall in uninsurance, receive job opportunity
                expprob = invmyprod[y]*mzinv[e]*mprod[tgrid][k]*(1.0 - phipar)*(1.0 - pLTpar)*(piE(searchUiE_J[inx(igrid, tgrid)]))*(piW( searchUiE_W[inx(igrid, tgrid)]));
                distJE[inxE(isaveUiE[inx(igrid, tgrid)], k, e)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JE_0 > (valueWWE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0));
                distJE[inxE(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, e)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JE_1 > (valueWWE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
                distUiEtoJE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JE_0 > (valueWWE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0)) + expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JE_1 > (valueWWE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));

            
                // TRANSITION TO ENTREPRENEUR NOT-EXCLUDED
                // receive phipar, not fall in uninsurance, receive job opportunity
                expprob = invmyprod[y]*mzinv[e]*mprod[tgrid][k]*(phipar)*(1.0 - pLTpar)*(piE(searchUiE_J[inx(igrid, tgrid)]))*(piW( searchUiE_W[inx(igrid, tgrid)]));
                distJNE[inxE(isaveUiE[inx(igrid, tgrid)], k, e)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_0 > (valueWWNE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0));
                distJNE[inxE(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, e)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_1 > (valueWWNE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
                distUiEtoJNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_0 > (valueWWNE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0)) + expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_1 > (valueWWNE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
                
                // not receive phipar, fall in uninsurance, receive job opportunity
                expprob = invmyprod[y]*mzinv[e]*mprod[tgrid][k]*(1.0 - phipar)*(pLTpar)*(piE(searchUiE_J[inx(igrid, tgrid)]))*(piW( searchUiE_W[inx(igrid, tgrid)]));
                distJE[inxE(isaveUiE[inx(igrid, tgrid)], k, e)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JE_0 > (valueWWE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0));
                distJE[inxE(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, e)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JE_1 > (valueWWE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
                distUnEtoJE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JE_0 > (valueWWE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0)) + expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JE_1 > (valueWWE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
                
                // receive phipar, fall in uninsurance, receive job opportunity
                expprob = invmyprod[y]*mzinv[e]*mprod[tgrid][k]*(phipar)*(pLTpar)*(piE(searchUiE_J[inx(igrid, tgrid)]))*(piW( searchUiE_W[inx(igrid, tgrid)]));
                distJNE[inxE(isaveUiE[inx(igrid, tgrid)], k, e)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_0 > (valueWWNE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0));
                distJNE[inxE(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, e)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_1 > (valueWWNE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
                distUnEtoJNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_0 > (valueWWNE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0)) + expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_1 > (valueWWNE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
                #endif
                
                
                #if SEA == 1
                // TRANSITION TO ENTREPRENEUR EXCLUDED
                
                // not receive phipar, not fall in uninsurance, receive job opportunity
                expprob = invmyprod[y]*mzinv[e]*mprod[tgrid][k]*(1.0 - phipar)*(1.0 - pLTpar)*(piE(searchUiE_J[inx(igrid, tgrid)]))*(piW( searchUiE_W[inx(igrid, tgrid)]));
                distJEi[inxE(isaveUiE[inx(igrid, tgrid)], k, e)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JEi_0 > (valueWWE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0));
                distJEi[inxE(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, e)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JEi_1 > (valueWWE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
                distUiEtoJE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JEi_0 > (valueWWE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0)) + expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JEi_1 > (valueWWE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
                
                if(tempUiE_JEi_0 >= (valueWWE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)]) & tempUiE_JE_0 <= (valueWWE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)])) {
                    distUiEtoJE_select[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JEi_0 > (valueWWE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0));
                }
                if(tempUiE_JEi_1 >= (valueWWE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)]) & tempUiE_JE_1 <= (valueWWE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)])) {
                    distUiEtoJE_select[inx(igrid, tgrid)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JEi_1 > (valueWWE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
                }
                
                // not receive phipar, fall in uninsurance, receive job opportunity
                expprob = invmyprod[y]*mzinv[e]*mprod[tgrid][k]*(1.0 - phipar)*(pLTpar)*(piE(searchUiE_J[inx(igrid, tgrid)]))*(piW( searchUiE_W[inx(igrid, tgrid)]));
                distJE[inxE(isaveUiE[inx(igrid, tgrid)], k, e)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JE_0 > (valueWWE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0));
                distJE[inxE(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, e)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JE_1 > (valueWWE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
                distUnEtoJE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JE_0 > (valueWWE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0)) + expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JE_1 > (valueWWE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
                
                
                // TRANSITION TO ENTREPRENEUR NOT-EXCLUDED
                // receive phipar, not fall in uninsurance, receive job opportunity
                expprob = invmyprod[y]*mzinv[e]*mprod[tgrid][k]*(phipar)*(1.0 - pLTpar)*(piE(searchUiE_J[inx(igrid, tgrid)]))*(piW( searchUiE_W[inx(igrid, tgrid)]));
                distJNEi[inxE(isaveUiE[inx(igrid, tgrid)], k, e)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNEi_0 > (valueWWNE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0));
                distJNEi[inxE(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, e)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNEi_1 > (valueWWNE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
                distUiEtoJNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNEi_0 > (valueWWNE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0)) + expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNEi_1 > (valueWWNE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));

                if(tempUiE_JNEi_0 >= (valueWWNE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)]) & tempUiE_JNE_0 <= (valueWWNE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)])) {
                    distUiEtoJNE_select[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNEi_0 > (valueWWNE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0));
                }
                if(tempUiE_JNEi_1 >= (valueWWNE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)]) & tempUiE_JNE_1 <= (valueWWNE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)])) {
                    distUiEtoJNE_select[inx(igrid, tgrid)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNEi_1 > (valueWWNE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
                }
                
                // receive phipar, fall in uninsurance, receive job opportunity
                expprob = invmyprod[y]*mzinv[e]*mprod[tgrid][k]*(phipar)*(pLTpar)*(piE(searchUiE_J[inx(igrid, tgrid)]))*(piW( searchUiE_W[inx(igrid, tgrid)]));
                distJNE[inxE(isaveUiE[inx(igrid, tgrid)], k, e)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_0 > (valueWWNE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0));
                distJNE[inxE(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, e)] += expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_1 > (valueWWNE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
                distUnEtoJNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_0 > (valueWWNE[inxW(isaveUiE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0)) + expprob*(saveresUiE[inx(igrid, tgrid)])*distUiEold[inx(igrid, tgrid)]*(((tempUiE_JNE_1 > (valueWWNE[inxW(min((maxgrid-1),(isaveUiE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
                #endif
                
                
                
                /*** UNEMPLOYED UNINSURED EXCLUDED ***/
                
                // NECESSITY SHARE //
                // not receive phipar, not fall in uninsurance, not receive job opportunity
                expprob = invmyprod[y]*mzinv[e]*mprod[tgrid][k]*(1.0 - phipar)*(piE(searchUnE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUnE_W[inx(igrid, tgrid)]));
                if(tempUnE_JE_0 >= valueUnE[inx(isaveUnE[inx(igrid, tgrid)], k)] & valueWWE[inxW(isaveUnE[inx(igrid, tgrid)], k, y)] >= tempUnE_JE_0) {
                    necessityUnE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)]*(((tempUnE_JE_0 >= (valueUnE[inx(isaveUnE[inx(igrid, tgrid)], k)])))?(1.0):(0.0));
                }
                if(tempUnE_JE_1 >= valueUnE[inx(min((maxgrid-1),(isaveUnE[inx(igrid, tgrid)]+1)), k)] & tempUnE_JE_1 <= valueWWE[inxW(min((maxgrid-1),(isaveUnE[inx(igrid, tgrid)]+1)), k, y)]) {
                    necessityUnE[inx(igrid, tgrid)] += expprob*(saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)]*(((tempUnE_JE_1 >= (valueUnE[inx(min((maxgrid-1),(isaveUnE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
                }
                
                // receive phipar, not fall in uninsurance, not receive job opportunity
                expprob = invmyprod[y]*mzinv[e]*mprod[tgrid][k]*(phipar)*(piE(searchUnE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUnE_W[inx(igrid, tgrid)]));
                if(tempUnE_JNE_0 >= valueUnNE[inx(isaveUnE[inx(igrid, tgrid)], k)] & valueWWNE[inxW(isaveUnE[inx(igrid, tgrid)], k, y)] >= tempUnE_JNE_0) {
                    necessityUnE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)]*(((tempUnE_JNE_0 >= (valueUnNE[inx(isaveUnE[inx(igrid, tgrid)], k)])))?(1.0):(0.0));
                }
                if(tempUnE_JNE_1 >= valueUnNE[inx(min((maxgrid-1),(isaveUnE[inx(igrid, tgrid)]+1)), k)] & tempUnE_JNE_1 <= valueWWNE[inxW(min((maxgrid-1),(isaveUnE[inx(igrid, tgrid)]+1)), k, y)]) {
                    necessityUnE[inx(igrid, tgrid)] += expprob*(saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)]*(((tempUnE_JNE_1 >= (valueUnNE[inx(min((maxgrid-1),(isaveUnE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
                }
                
                
            
                // TRANSITION TO ENTREPRENEUR EXCLUDED
                
                // not receive phipar, not fall in uninsurance, receive job opportunity
                expprob = invmyprod[y]*mzinv[e]*mprod[tgrid][k]*(1.0 - phipar)*(piE(searchUnE_J[inx(igrid, tgrid)]))*(piW( searchUnE_W[inx(igrid, tgrid)]));
                distJE[inxE(isaveUnE[inx(igrid, tgrid)], k, e)] += expprob*(1.0 - saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)]*(((tempUnE_JE_0 > (valueWWE[inxW(isaveUnE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0));
                distJE[inxE(min((maxgrid-1),(isaveUnE[inx(igrid, tgrid)]+1)), k, e)] += expprob*(saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)]*(((tempUnE_JE_1 > (valueWWE[inxW(min((maxgrid-1),(isaveUnE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
                distUnEtoJE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)]*(((tempUnE_JE_0 > (valueWWE[inxW(isaveUnE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0)) + expprob*(saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)]*(((tempUnE_JE_1 > (valueWWE[inxW(min((maxgrid-1),(isaveUnE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));


                // TRANSITION TO ENTREPRENEUR NOT-EXCLUDED
            
                // receive phipar, not fall in uninsurance, receive job opportunity
                expprob = invmyprod[y]*mzinv[e]*mprod[tgrid][k]*(phipar)*(piE(searchUnE_J[inx(igrid, tgrid)]))*(piW( searchUnE_W[inx(igrid, tgrid)]));
                distJNE[inxE(isaveUnE[inx(igrid, tgrid)], k, e)] += expprob*(1.0 - saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)]*(((tempUnE_JNE_0 > (valueWWNE[inxW(isaveUnE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0));
                distJNE[inxE(min((maxgrid-1),(isaveUnE[inx(igrid, tgrid)]+1)), k, e)] += expprob*(saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)]*(((tempUnE_JNE_1 > (valueWWNE[inxW(min((maxgrid-1),(isaveUnE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
                distUnEtoJNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)]*(((tempUnE_JNE_0 > (valueWWNE[inxW(isaveUnE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0)) + expprob*(saveresUnE[inx(igrid, tgrid)])*distUnEold[inx(igrid, tgrid)]*(((tempUnE_JNE_1 > (valueWWNE[inxW(min((maxgrid-1),(isaveUnE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
                
                
                
                /*** UNEMPLOYED INSURED NOT EXCLUDED ***/
            
                // NECESSITY SHARE //
                // not fall in uninsurance, not receive job opportunity
                #if NOPOLICY == 1
                expprob = invmyprod[y]*mzinv[e]*mprod[tgrid][k]*(1.0 - pLTpar)*(piE(searchUiNE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUiNE_W[inx(igrid, tgrid)]));
                if(tempUiNE_JNE_0 >= valueUiNE[inx(isaveUiNE[inx(igrid, tgrid)], k)] & valueWWNE[inxW(isaveUiNE[inx(igrid, tgrid)], k, y)] >= tempUiNE_JNE_0) {
                    necessityUiNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)];
                }
                if(tempUiNE_JNE_1 >= valueUiNE[inx(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k)] & tempUiNE_JNE_1 <= valueWWNE[inxW(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k, y)]) {
                    necessityUiNE[inx(igrid, tgrid)] += expprob*(saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)];
                }
                #endif
                
                
                 // not fall in uninsurance, not receive job opportunity
                #if SEA == 1
                expprob = invmyprod[y]*mzinv[e]*mprod[tgrid][k]*(1.0 - pLTpar)*(piE(searchUiNE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUiNE_W[inx(igrid, tgrid)]));
                if((tempUiNE_JNEi_0*(1.0-alt_def_necessity_SEA) + alt_def_necessity_SEA*tempUiNE_JNE_0) >= valueUiNE[inx(isaveUiNE[inx(igrid, tgrid)], k)] & valueWWNE[inxW(isaveUiNE[inx(igrid, tgrid)], k, y)] >= (tempUiNE_JNEi_0*(1.0-alt_def_necessity_SEA) + alt_def_necessity_SEA*tempUiNE_JNE_0)) {
                    necessityUiNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)];
                }
                if((tempUiNE_JNEi_1*(1.0-alt_def_necessity_SEA) + alt_def_necessity_SEA*tempUiNE_JNE_1) >= valueUiNE[inx(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k)] & (tempUiNE_JNEi_1*(1.0-alt_def_necessity_SEA) + alt_def_necessity_SEA*tempUiNE_JNE_1) <= valueWWNE[inxW(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k, y)]) {
                    necessityUiNE[inx(igrid, tgrid)] += expprob*(saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)];
                }
                #endif
                
                // fall in uninsurance, not receive job opportunity
                expprob = invmyprod[y]*mzinv[e]*mprod[tgrid][k]*(pLTpar)*(piE(searchUiNE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUiNE_W[inx(igrid, tgrid)]));
                if(tempUiNE_JNE_0 >= valueUnNE[inx(isaveUiNE[inx(igrid, tgrid)], k)] & valueWWNE[inxW(isaveUiNE[inx(igrid, tgrid)], k, y)] >= tempUiNE_JNE_0) {
                    necessityUiNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)];
                }
                if(tempUiNE_JNE_1 >= valueUnNE[inx(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k)] & tempUiNE_JNE_1 <= valueWWNE[inxW(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k, y)]) {
                    necessityUiNE[inx(igrid, tgrid)] += expprob*(saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)];
                }
            
            

                // TRANSITION TO ENTREPRENEUR NOT-EXCLUDED
                // not fall in uninsurance, receive job opportunity
                #if NOPOLICY == 1
                expprob = invmyprod[y]*mzinv[e]*mprod[tgrid][k]*(1.0 - pLTpar)*(piE(searchUiNE_J[inx(igrid, tgrid)]))*(piW( searchUiNE_W[inx(igrid, tgrid)]));
                distJNE[inxE(isaveUiNE[inx(igrid, tgrid)], k, e)] += expprob*(1.0 - saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNE_0 > (valueWWNE[inxW(isaveUiNE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0));
                distJNE[inxE(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k, e)] += expprob*(saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNE_1 > (valueWWNE[inxW(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
                distUiNEtoJNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNE_0 > (valueWWNE[inxW(isaveUiNE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0)) + expprob*(saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNE_1 > (valueWWNE[inxW(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
                #endif

                #if SEA == 1
                expprob = invmyprod[y]*mzinv[e]*mprod[tgrid][k]*(1.0 - pLTpar)*(piE(searchUiNE_J[inx(igrid, tgrid)]))*(piW( searchUiNE_W[inx(igrid, tgrid)]));
                distJNEi[inxE(isaveUiNE[inx(igrid, tgrid)], k, e)] += expprob*(1.0 - saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNEi_0 > (valueWWNE[inxW(isaveUiNE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0));
                distJNEi[inxE(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k, e)] += expprob*(saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNEi_1 > (valueWWNE[inxW(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
                distUiNEtoJNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNEi_0 > (valueWWNE[inxW(isaveUiNE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0)) + expprob*(saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNEi_1 > (valueWWNE[inxW(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
                #endif
                
                
                // fall in uninsurance, receive job opportunity
                expprob = invmyprod[y]*mzinv[e]*mprod[tgrid][k]*(pLTpar)*(piE(searchUiNE_J[inx(igrid, tgrid)]))*(piW( searchUiNE_W[inx(igrid, tgrid)]));
                distJNE[inxE(isaveUiNE[inx(igrid, tgrid)], k, e)] += expprob*(1.0 - saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNE_0 > (valueWWNE[inxW(isaveUiNE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0));
                distJNE[inxE(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k, e)] += expprob*(saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNE_1 > (valueWWNE[inxW(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
                distUiNEtoJNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNE_0 > (valueWWNE[inxW(isaveUiNE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0)) + expprob*(saveresUiNE[inx(igrid, tgrid)])*distUiNEold[inx(igrid, tgrid)]*(((tempUiNE_JNE_1 > (valueWWNE[inxW(min((maxgrid-1),(isaveUiNE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
                
                
                
                /*** UNEMPLOYED UNINSURED NOT EXCLUDED ***/
                // NECESSITY SHARE //
                // fall in uninsurance, not receive job opportunity
                expprob = invmyprod[y]*mzinv[e]*mprod[tgrid][k]*(piE(searchUnNE_J[inx(igrid, tgrid)]))*(1.0 - piW( searchUnNE_W[inx(igrid, tgrid)]));
                if(tempUnNE_JNE_0 >= valueUnNE[inx(isaveUnNE[inx(igrid, tgrid)], k)] & valueWWNE[inxW(isaveUnNE[inx(igrid, tgrid)], k, y)] >= tempUnNE_JNE_0) {
                    necessityUiNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUnNE[inx(igrid, tgrid)])*distUnNEold[inx(igrid, tgrid)]*(((tempUnNE_JNE_0 >= (valueUnNE[inx(isaveUnNE[inx(igrid, tgrid)], k)])))?(1.0):(0.0));
                }
                if(tempUnNE_JNE_1 >= valueUnNE[inx(min((maxgrid-1),(isaveUnNE[inx(igrid, tgrid)]+1)), k)] & tempUnNE_JNE_1 <= valueWWNE[inxW(min((maxgrid-1),(isaveUnNE[inx(igrid, tgrid)]+1)), k, y)]) {
                    necessityUiNE[inx(igrid, tgrid)] += expprob*(saveresUnNE[inx(igrid, tgrid)])*distUnNEold[inx(igrid, tgrid)]*(((tempUnNE_JNE_1 >= (valueUnNE[inx(min((maxgrid-1),(isaveUnNE[inx(igrid, tgrid)]+1)), k)])))?(1.0):(0.0));
                }
                

                // TRANSITION TO ENTREPRENEUR NOT-EXCLUDED
                // not fall in uninsurance, receive job opportunity
                expprob = invmyprod[y]*mzinv[e]*mprod[tgrid][k]*(piE(searchUnNE_J[inx(igrid, tgrid)]))*(piW( searchUnNE_W[inx(igrid, tgrid)]));
                distJNE[inxE(isaveUnNE[inx(igrid, tgrid)], k, e)] += expprob*(1.0 - saveresUnNE[inx(igrid, tgrid)])*distUnNEold[inx(igrid, tgrid)]*(((tempUnNE_JNE_0 > (valueWWNE[inxW(isaveUnNE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0));
                distJNE[inxE(min((maxgrid-1),(isaveUnNE[inx(igrid, tgrid)]+1)), k, e)] += expprob*(saveresUnNE[inx(igrid, tgrid)])*distUnNEold[inx(igrid, tgrid)]*(((tempUnNE_JNE_1 > (valueWWNE[inxW(min((maxgrid-1),(isaveUnNE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
                distUnNEtoJNE[inx(igrid, tgrid)] += expprob*(1.0 - saveresUnNE[inx(igrid, tgrid)])*distUnNEold[inx(igrid, tgrid)]*(((tempUnNE_JNE_0 > (valueWWNE[inxW(isaveUnNE[inx(igrid, tgrid)], k, y)])))?(1.0):(0.0)) + expprob*(saveresUnNE[inx(igrid, tgrid)])*distUnNEold[inx(igrid, tgrid)]*(((tempUnNE_JNE_1 > (valueWWNE[inxW(min((maxgrid-1),(isaveUnNE[inx(igrid, tgrid)]+1)), k, y)])))?(1.0):(0.0));
                
                
            } // end y

        } // end e
        
        



            
        for(zgrid = 0; zgrid < maxfirmtype; zgrid++){
            for(e = 0; e < maxfirmtype; e++) {
            
                #if SEA == 1
                
                    /*****************************************/
                    /*** ENTREPRENEUR INSURED NOT-EXCLUDED ***/
                    /*****************************************/

                    // if the entrepreneur default
                    if(defaultJNEi[inxEE(igrid, tgrid, zgrid, e)] == 1) {
                        
                        // TRANSITION ENT TO WORKER EXCLUDED (receive job offer)
                        for(y=0; y < maxyprod; y++) {
                            
                            // continue to receive unemployment insurance
                            expprob = invmyprod[y]*mprod[tgrid][k]*(1.0 - MUENDOvecB[inxEE(igrid,tgrid,zgrid,e)])*mzprob[zgrid][e]*(piW( searchJNEi_W[inxEE(igrid,tgrid,zgrid,e)]));
                            distWWE[inxW(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)], k, y)] += expprob*(1.0 - saveresJNEi[inxEE(igrid, tgrid, zgrid, e)])*distJNEiold[inxE(igrid, tgrid, zgrid)];
                            distWWE[inxW(min((maxgrid-1),(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, y)] += expprob*(saveresJNEi[inxEE(igrid, tgrid, zgrid, e)])*distJNEiold[inxE(igrid, tgrid, zgrid)];
                            distJNEtoWWE[inxE(igrid, tgrid, zgrid)] += expprob*(1.0 - saveresJNEi[inxEE(igrid, tgrid, zgrid, e)])*distJNEiold[inxE(igrid, tgrid, zgrid)] + expprob*(saveresJNEi[inxEE(igrid, tgrid, zgrid, e)])*distJNEiold[inxE(igrid, tgrid, zgrid)];
                            

                            // lost rights
                            expprob = invmyprod[y]*mprod[tgrid][k]*(MUENDOvecB[inxEE(igrid,tgrid,zgrid,e)])*mzprob[zgrid][e]*(piW( searchJNEi_W[inxEE(igrid,tgrid,zgrid,e)]));
                            distWWE[inxW(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)], k, y)] += expprob*(1.0 - saveresJNEi[inxEE(igrid, tgrid, zgrid, e)])*distJNEiold[inxE(igrid, tgrid, zgrid)];
                            distWWE[inxW(min((maxgrid-1),(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, y)] += expprob*(saveresJNEi[inxEE(igrid, tgrid, zgrid, e)])*distJNEiold[inxE(igrid, tgrid, zgrid)];
                            distJNEtoWWE[inxE(igrid, tgrid, zgrid)] += expprob*(1.0 - saveresJNEi[inxEE(igrid, tgrid, zgrid, e)])*distJNEiold[inxE(igrid, tgrid, zgrid)] + expprob*(saveresJNEi[inxEE(igrid, tgrid, zgrid, e)])*distJNEiold[inxE(igrid, tgrid, zgrid)];
                            

                        }
                        
                        
                        // TRANSITION ENT TO UNEMPLOYED ST EXCLUDED // does not lost his rights
                        expprob = mprod[tgrid][k]*mzprob[zgrid][e]*(1.0 - MUENDOvecB[inxEE(igrid,tgrid,zgrid,e)])*(1.0 - piW( searchJNEi_W[inxEE(igrid,tgrid,zgrid, e)]));
                        distUiE[inx(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)], k)] += expprob*(1.0 - saveresJNEi[inxEE(igrid, tgrid, zgrid, e)])*distJNEiold[inxE(igrid, tgrid, zgrid)];
                        distUiE[inx(min((maxgrid-1),(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k)] += expprob*(saveresJNEi[inxEE(igrid, tgrid, zgrid, e)])*distJNEiold[inxE(igrid, tgrid, zgrid)];
                        distJNEtoUiE[inxE(igrid, tgrid, zgrid)] += expprob*(1.0 - saveresJNEi[inxEE(igrid, tgrid, zgrid, e)])*distJNEiold[inxE(igrid, tgrid, zgrid)] + expprob*(saveresJNEi[inxEE(igrid, tgrid, zgrid, e)])*distJNEiold[inxE(igrid, tgrid, zgrid)];


                        // TRANSITION ENT TO UNEMPLOYED ST EXCLUDED // lost his rights
                        expprob = mprod[tgrid][k]*mzprob[zgrid][e]*(MUENDOvecB[inxEE(igrid,tgrid,zgrid,e)])*(1.0 - piW( searchJNEi_W[inxEE(igrid,tgrid,zgrid, e)]));
                        distUnE[inx(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)], k)] += expprob*(1.0 - saveresJNEi[inxEE(igrid, tgrid, zgrid, e)])*distJNEiold[inxE(igrid, tgrid, zgrid)];
                        distUnE[inx(min((maxgrid-1),(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k)] += expprob*(saveresJNEi[inxEE(igrid, tgrid, zgrid, e)])*distJNEiold[inxE(igrid, tgrid, zgrid)];
                        distJNEtoUnE[inxE(igrid, tgrid, zgrid)] += expprob*(1.0 - saveresJNEi[inxEE(igrid, tgrid, zgrid, e)])*distJNEiold[inxE(igrid, tgrid, zgrid)] + expprob*(saveresJNEi[inxEE(igrid, tgrid, zgrid, e)])*distJNEiold[inxE(igrid, tgrid, zgrid)];


                    }



                    // if the entrepreneur repay
                    if(defaultJNEi[inxEE(igrid, tgrid, zgrid, e)] == 0) {
                        
                        // TRANSITION ENT TO WORKER NOT EXCLUDED (receive job offer)
                        for(y=0; y < maxyprod; y++) {
                        
                            // does not lost his rights
                            expprob = invmyprod[y]*mprod[tgrid][k]*(1.0 - MUENDOvecR[inxEE(igrid,tgrid,zgrid,e)])*mzprob[zgrid][e]*(piW( searchJNEi_W[inxEE(igrid,tgrid,zgrid, e)]));
                            distWWNE[inxW(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)], k, y)] += expprob*(1.0 - saveresJNEi[inxEE(igrid, tgrid, zgrid, e)])*distJNEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNEi[inxE(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] <= (valueWWNE[inxW(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)], k, y)])))?(1.0):(0.0));
                            distWWNE[inxW(min((maxgrid-1),(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, y)] += expprob*(saveresJNEi[inxEE(igrid, tgrid, zgrid, e)])*distJNEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNEi[inxE(min((maxgrid-1),(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] <= (valueWWNE[inxW(min((maxgrid-1),(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, y)])))?(1.0):(0.0));
                            distJNEtoWWNE[inxE(igrid, tgrid, zgrid)] += expprob*(1.0 - saveresJNEi[inxEE(igrid, tgrid, zgrid, e)])*distJNEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNEi[inxE(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] <= (valueWWNE[inxW(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)], k, y)])))?(1.0):(0.0)) + expprob*(saveresJNEi[inxEE(igrid, tgrid, zgrid, e)])*distJNEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNEi[inxE(min((maxgrid-1),(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] <= (valueWWNE[inxW(min((maxgrid-1),(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, y)])))?(1.0):(0.0));
                            
                            // does not lost his rights // residual who stay entrepreneur
                            expprob = invmyprod[y]*mprod[tgrid][k]*(1.0 - MUENDOvecR[inxEE(igrid,tgrid,zgrid,e)])*mzprob[zgrid][e]*(piW( searchJNEi_W[inxEE(igrid,tgrid,zgrid, e)]));
                            distJNEi[inxE(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] += expprob*(1.0 - saveresJNEi[inxEE(igrid, tgrid, zgrid, e)])*distJNEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNEi[inxE(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] > (valueWWNE[inxW(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)], k, y)])))?(1.0):(0.0));
                            distJNEi[inxE(min((maxgrid-1),(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] += expprob*(saveresJNEi[inxEE(igrid, tgrid, zgrid, e)])*distJNEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNEi[inxE(min((maxgrid-1),(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] > (valueWWNE[inxW(min((maxgrid-1),(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, y)])))?(1.0):(0.0));
                        
                             // lost his rights
                            expprob = invmyprod[y]*mprod[tgrid][k]*(MUENDOvecR[inxEE(igrid,tgrid,zgrid,e)])*mzprob[zgrid][e]*(piW( searchJNEi_W[inxEE(igrid,tgrid,zgrid, e)]));
                            distWWNE[inxW(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)], k, y)] += expprob*(1.0 - saveresJNEi[inxEE(igrid, tgrid, zgrid, e)])*distJNEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] <= (valueWWNE[inxW(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)], k, y)])))?(1.0):(0.0));
                            distWWNE[inxW(min((maxgrid-1),(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, y)] += expprob*(saveresJNEi[inxEE(igrid, tgrid, zgrid, e)])*distJNEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(min((maxgrid-1),(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] <= (valueWWNE[inxW(min((maxgrid-1),(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, y)])))?(1.0):(0.0));
                            distJNEtoWWNE[inxE(igrid, tgrid, zgrid)] += expprob*(1.0 - saveresJNEi[inxEE(igrid, tgrid, zgrid, e)])*distJNEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] <= (valueWWNE[inxW(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)], k, y)])))?(1.0):(0.0)) + expprob*(saveresJNEi[inxEE(igrid, tgrid, zgrid, e)])*distJNEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(min((maxgrid-1),(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] <= (valueWWNE[inxW(min((maxgrid-1),(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, y)])))?(1.0):(0.0));
                            
                            // lost his rights // residual who stay entrepreneur
                            expprob = invmyprod[y]*mprod[tgrid][k]*(MUENDOvecR[inxEE(igrid,tgrid,zgrid,e)])*mzprob[zgrid][e]*(piW( searchJNEi_W[inxEE(igrid,tgrid,zgrid, e)]));
                            distJNE[inxE(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] += expprob*(1.0 - saveresJNEi[inxEE(igrid, tgrid, zgrid, e)])*distJNEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] > (valueWWNE[inxW(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)], k, y)])))?(1.0):(0.0));
                            distJNE[inxE(min((maxgrid-1),(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] += expprob*(saveresJNEi[inxEE(igrid, tgrid, zgrid, e)])*distJNEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(min((maxgrid-1),(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] > (valueWWNE[inxW(min((maxgrid-1),(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, y)])))?(1.0):(0.0));
                        
                        }
                        

                        // TRANSITION ENT TO UNINSURED UNEMPLOYED NOT-EXCLUDED // does not lost his rights
                        expprob = mprod[tgrid][k]*mzprob[zgrid][e]*(1.0 - MUENDOvecR[inxEE(igrid,tgrid,zgrid,e)])*(1.0 - piW( searchJNEi_W[inxEE(igrid,tgrid,zgrid, e)]));
                        distUiNE[inx(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)], k)] += expprob*(1.0 - saveresJNEi[inxEE(igrid, tgrid, zgrid, e)])*distJNEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNEi[inxE(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] < (valueUiNE[inx(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)], k)])))?(1.0):(0.0));
                        distUiNE[inx(min((maxgrid-1),(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k)] += expprob*(saveresJNEi[inxEE(igrid, tgrid, zgrid, e)])*distJNEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNEi[inxE(min((maxgrid-1),(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] < (valueUiNE[inx(min((maxgrid-1),(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k)])))?(1.0):(0.0));
                        distJNEtoUiNE[inxE(igrid, tgrid, zgrid)] += expprob*(1.0 - saveresJNEi[inxEE(igrid, tgrid, zgrid, e)])*distJNEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNEi[inxE(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] < (valueUiNE[inx(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)], k)])))?(1.0):(0.0)) + expprob*(saveresJNEi[inxEE(igrid, tgrid, zgrid, e)])*distJNEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNEi[inxE(min((maxgrid-1),(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] < (valueUiNE[inx(min((maxgrid-1),(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k)])))?(1.0):(0.0));
                        
                        // residual who stay entrepreneur
                        expprob = mprod[tgrid][k]*mzprob[zgrid][e]*(1.0 - MUENDOvecR[inxEE(igrid,tgrid,zgrid,e)])*(1.0 - piW( searchJNEi_W[inxEE(igrid,tgrid,zgrid, e)]));
                        distJNEi[inxE(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] += expprob*(1.0 - saveresJNEi[inxEE(igrid, tgrid, zgrid, e)])*distJNEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNEi[inxE(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] >= (valueUiNE[inx(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)], k)])))?(1.0):(0.0));
                        distJNEi[inxE(min((maxgrid-1),(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] += expprob*(saveresJNEi[inxEE(igrid, tgrid, zgrid, e)])*distJNEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNEi[inxE(min((maxgrid-1),(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] >= (valueUiNE[inx(min((maxgrid-1),(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k)])))?(1.0):(0.0));
                        
                        
                         // TRANSITION ENT TO UNINSURED UNEMPLOYED NOT-EXCLUDED // lose his rights
                        expprob = mprod[tgrid][k]*mzprob[zgrid][e]*(MUENDOvecR[inxEE(igrid,tgrid,zgrid,e)])*(1.0 - piW( searchJNEi_W[inxEE(igrid,tgrid,zgrid, e)]));
                        distUnNE[inx(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)], k)] += expprob*(1.0 - saveresJNEi[inxEE(igrid, tgrid, zgrid, e)])*distJNEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] < (valueUnNE[inx(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)], k)])))?(1.0):(0.0));
                        distUnNE[inx(min((maxgrid-1),(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k)] += expprob*(saveresJNEi[inxEE(igrid, tgrid, zgrid, e)])*distJNEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(min((maxgrid-1),(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] < (valueUnNE[inx(min((maxgrid-1),(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k)])))?(1.0):(0.0));
                        distJNEtoUnNE[inxE(igrid, tgrid, zgrid)] += expprob*(1.0 - saveresJNEi[inxEE(igrid, tgrid, zgrid, e)])*distJNEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] < (valueUnNE[inx(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)], k)])))?(1.0):(0.0)) + expprob*(saveresJNEi[inxEE(igrid, tgrid, zgrid, e)])*distJNEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(min((maxgrid-1),(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] < (valueUnNE[inx(min((maxgrid-1),(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k)])))?(1.0):(0.0));
                        
                        // residual who stay entrepreneur
                        expprob = mprod[tgrid][k]*mzprob[zgrid][e]*(MUENDOvecR[inxEE(igrid,tgrid,zgrid,e)])*(1.0 - piW( searchJNEi_W[inxEE(igrid,tgrid,zgrid, e)]));
                        distJNE[inxE(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] += expprob*(1.0 - saveresJNEi[inxEE(igrid, tgrid, zgrid, e)])*distJNEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] >= (valueUnNE[inx(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)], k)])))?(1.0):(0.0));
                        distJNE[inxE(min((maxgrid-1),(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] += expprob*(saveresJNEi[inxEE(igrid, tgrid, zgrid, e)])*distJNEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(min((maxgrid-1),(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] >= (valueUnNE[inx(min((maxgrid-1),(isaveJNEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k)])))?(1.0):(0.0));
                        
                    } // end condition




                    /*****************************/
                    /*** ENTREPRENEUR INSURED EXCLUDED ***/
                    /*****************************/

                    for(y=0; y < maxyprod; y++) {

                        // TRANSITION ENT TO WORKER EXCLUDED (receive job offer, not receive phipar) // does not lose his rights
                        expprob = invmyprod[y]*mprod[tgrid][k]*mzprob[zgrid][e]*(1.0 - MUENDOvecEhat[inxEE(igrid,tgrid,zgrid,e)])*(1.0 - phipar)*(piW( searchJEi_W[inxEE(igrid,tgrid,zgrid, e)]));
                        distWWE[inxW(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, y)] += expprob*(1.0 - saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJEi[inxE(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] <= (valueWWE[inxW(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, y)])))?(1.0):(0.0));
                        distWWE[inxW(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, y)] += expprob*(saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJEi[inxE(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] <= (valueWWE[inxW(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, y)])))?(1.0):(0.0));
                        distJEtoWWE[inxE(igrid, tgrid, zgrid)] += expprob*(1.0 - saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJEi[inxE(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] <= (valueWWE[inxW(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, y)])))?(1.0):(0.0)) + expprob*(saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJEi[inxE(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] <= (valueWWE[inxW(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, y)])))?(1.0):(0.0));
                        
                        // and residual who stay entrepreneur
                        expprob = invmyprod[y]*mprod[tgrid][k]*mzprob[zgrid][e]*(1.0 - MUENDOvecEhat[inxEE(igrid,tgrid,zgrid,e)])*(1.0 - phipar)*(piW( searchJEi_W[inxEE(igrid,tgrid,zgrid, e)]));
                        distJEi[inxE(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] += expprob*(1.0 - saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJEi[inxE(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] > (valueWWE[inxW(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, y)])))?(1.0):(0.0));
                        distJEi[inxE(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] += expprob*(saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJEi[inxE(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] > (valueWWE[inxW(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, y)])))?(1.0):(0.0));
                        
                        
                        // TRANSITION ENT TO WORKER NOT-EXCLUDED (receive job offer, receive phipar)
                        expprob = invmyprod[y]*mprod[tgrid][k]*mzprob[zgrid][e]*(1.0 - MUENDOvecEhat[inxEE(igrid,tgrid,zgrid,e)])*(phipar)*(piW( searchJEi_W[inxEE(igrid,tgrid,zgrid, e)]));
                        distWWNE[inxW(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, y)] += expprob*(1.0 - saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNEi[inxE(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] <= (valueWWNE[inxW(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, y)])))?(1.0):(0.0));
                        distWWNE[inxW(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, y)] += expprob*(saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNEi[inxE(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] <= (valueWWNE[inxW(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, y)])))?(1.0):(0.0));
                        distJEtoWWNE[inxE(igrid, tgrid, zgrid)] += expprob*(1.0 - saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNEi[inxE(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] <= (valueWWNE[inxW(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, y)])))?(1.0):(0.0)) + expprob*(saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNEi[inxE(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] <= (valueWWNE[inxW(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, y)])))?(1.0):(0.0));
                        
                        // and residual who stay entrepreneur
                        expprob = invmyprod[y]*mprod[tgrid][k]*mzprob[zgrid][e]*(1.0 - MUENDOvecEhat[inxEE(igrid,tgrid,zgrid,e)])*(phipar)*(piW( searchJEi_W[inxEE(igrid,tgrid,zgrid, e)]));
                        distJNEi[inxE(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] += expprob*(1.0 - saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNEi[inxE(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] > (valueWWNE[inxW(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, y)])))?(1.0):(0.0));
                        distJNEi[inxE(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] += expprob*(saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNEi[inxE(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] > (valueWWNE[inxW(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, y)])))?(1.0):(0.0));
                        



                        // TRANSITION ENT TO WORKER EXCLUDED (receive job offer, not receive phipar) //  lose his rights
                        expprob = invmyprod[y]*mprod[tgrid][k]*mzprob[zgrid][e]*(MUENDOvecEhat[inxEE(igrid,tgrid,zgrid,e)])*(1.0 - phipar)*(piW( searchJEi_W[inxEE(igrid,tgrid,zgrid, e)]));
                        distWWE[inxW(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, y)] += expprob*(1.0 - saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJE[inxE(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] <= (valueWWE[inxW(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, y)])))?(1.0):(0.0));
                        distWWE[inxW(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, y)] += expprob*(saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJE[inxE(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] <= (valueWWE[inxW(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, y)])))?(1.0):(0.0));
                        distJEtoWWE[inxE(igrid, tgrid, zgrid)] += expprob*(1.0 - saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJE[inxE(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] <= (valueWWE[inxW(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, y)])))?(1.0):(0.0)) + expprob*(saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJE[inxE(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] <= (valueWWE[inxW(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, y)])))?(1.0):(0.0));
                        
                        // and residual who stay entrepreneur
                        expprob = invmyprod[y]*mprod[tgrid][k]*mzprob[zgrid][e]*(MUENDOvecEhat[inxEE(igrid,tgrid,zgrid,e)])*(1.0 - phipar)*(piW( searchJEi_W[inxEE(igrid,tgrid,zgrid, e)]));
                        distJE[inxE(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] += expprob*(1.0 - saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJE[inxE(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] > (valueWWE[inxW(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, y)])))?(1.0):(0.0));
                        distJE[inxE(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] += expprob*(saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJE[inxE(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] > (valueWWE[inxW(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, y)])))?(1.0):(0.0));
                        
                        
                        // TRANSITION ENT TO WORKER NOT-EXCLUDED (receive job offer, receive phipar)
                        expprob = invmyprod[y]*mprod[tgrid][k]*mzprob[zgrid][e]*(MUENDOvecEhat[inxEE(igrid,tgrid,zgrid,e)])*(phipar)*(piW( searchJEi_W[inxEE(igrid,tgrid,zgrid, e)]));
                        distWWNE[inxW(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, y)] += expprob*(1.0 - saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] <= (valueWWNE[inxW(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, y)])))?(1.0):(0.0));
                        distWWNE[inxW(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, y)] += expprob*(saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] <= (valueWWNE[inxW(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, y)])))?(1.0):(0.0));
                        distJEtoWWNE[inxE(igrid, tgrid, zgrid)] += expprob*(1.0 - saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] <= (valueWWNE[inxW(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, y)])))?(1.0):(0.0)) + expprob*(saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] <= (valueWWNE[inxW(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, y)])))?(1.0):(0.0));
                        
                        // and residual who stay entrepreneur
                        expprob = invmyprod[y]*mprod[tgrid][k]*mzprob[zgrid][e]*(MUENDOvecEhat[inxEE(igrid,tgrid,zgrid,e)])*(phipar)*(piW( searchJEi_W[inxEE(igrid,tgrid,zgrid, e)]));
                        distJNE[inxE(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] += expprob*(1.0 - saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] > (valueWWNE[inxW(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, y)])))?(1.0):(0.0));
                        distJNE[inxE(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] += expprob*(saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] > (valueWWNE[inxW(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, y)])))?(1.0):(0.0));
                    }


                    // TRANSITION ENT TO UNEMPLOYED LT EXCLUDED  // does not lose his rights
                    // TRANSITION ENT TO UNINSURED UNEMPLOYED EXCLUDED (receive no job offer, not receive phipar)
                    expprob = mprod[tgrid][k]*mzprob[zgrid][e]*(1.0 - MUENDOvecEhat[inxEE(igrid,tgrid,zgrid,e)])*(1.0 - phipar)*(1.0 - piW( searchJEi_W[inxEE(igrid,tgrid,zgrid, e)]));
                    distUiE[inx(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k)] += expprob*(1.0 - saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJEi[inxE(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] < (valueUiE[inx(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k)])))?(1.0):(0.0));
                    distUiE[inx(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k)] += expprob*(saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJEi[inxE(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] < (valueUiE[inx(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k)])))?(1.0):(0.0));
                    distJEtoUiE[inxE(igrid, tgrid, zgrid)] += expprob*(1.0 - saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJEi[inxE(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] < (valueUiE[inx(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k)])))?(1.0):(0.0)) + expprob*(saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJEi[inxE(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] < (valueUiE[inx(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k)])))?(1.0):(0.0));

                    // and residual who star entrepreneur
                    expprob = mprod[tgrid][k]*mzprob[zgrid][e]*(1.0 - MUENDOvecEhat[inxEE(igrid,tgrid,zgrid,e)])*(1.0 - phipar)*(1.0 - piW( searchJEi_W[inxEE(igrid,tgrid,zgrid, e)]));
                    distJEi[inxE(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] += expprob*(1.0 - saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJEi[inxE(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] >= (valueUiE[inx(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k)])))?(1.0):(0.0));
                    distJEi[inxE(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] += expprob*(saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJEi[inxE(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] >= (valueUiE[inx(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k)])))?(1.0):(0.0));
                        
                    // TRANSITION ENT TO UNINSURED UNEMPLOYED NOT-EXCLUDED (not receive job offer, receive phipar)
                    expprob = mprod[tgrid][k]*mzprob[zgrid][e]*(1.0 - MUENDOvecEhat[inxEE(igrid,tgrid,zgrid,e)])*(phipar)*(1.0 - piW( searchJEi_W[inxEE(igrid,tgrid,zgrid, e)]));
                    distUiNE[inx(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k)] += expprob*(1.0 - saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNEi[inxE(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] < (valueUiNE[inx(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k)])))?(1.0):(0.0));
                    distUiNE[inx(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k)] += expprob*(saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNEi[inxE(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] < (valueUiNE[inx(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k)])))?(1.0):(0.0));
                    distJEtoUiNE[inxE(igrid, tgrid, zgrid)] += expprob*(1.0 - saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNEi[inxE(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] < (valueUiNE[inx(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k)])))?(1.0):(0.0)) + expprob*(saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNEi[inxE(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] < (valueUiNE[inx(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k)])))?(1.0):(0.0));

                    // and residual who star entrepreneur
                    expprob = mprod[tgrid][k]*mzprob[zgrid][e]*(1.0 - MUENDOvecEhat[inxEE(igrid,tgrid,zgrid,e)])*(phipar)*(1.0 - piW( searchJEi_W[inxEE(igrid,tgrid,zgrid, e)]));
                    distJNEi[inxE(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] += expprob*(1.0 - saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNEi[inxE(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] >= (valueUiNE[inx(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k)])))?(1.0):(0.0));
                    distJNEi[inxE(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] += expprob*(saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNEi[inxE(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] >= (valueUiNE[inx(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k)])))?(1.0):(0.0));



                    // TRANSITION ENT TO UNEMPLOYED LT EXCLUDED  // lose his rights
                    // TRANSITION ENT TO UNINSURED UNEMPLOYED EXCLUDED (receive no job offer, not receive phipar)
                    expprob = mprod[tgrid][k]*mzprob[zgrid][e]*(MUENDOvecEhat[inxEE(igrid,tgrid,zgrid,e)])*(1.0 - phipar)*(1.0 - piW( searchJEi_W[inxEE(igrid,tgrid,zgrid, e)]));
                    distUnE[inx(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k)] += expprob*(1.0 - saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJE[inxE(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] < (valueUnE[inx(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k)])))?(1.0):(0.0));
                    distUnE[inx(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k)] += expprob*(saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJE[inxE(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] < (valueUnE[inx(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k)])))?(1.0):(0.0));
                    distJEtoUnE[inxE(igrid, tgrid, zgrid)] += expprob*(1.0 - saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJE[inxE(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] < (valueUnE[inx(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k)])))?(1.0):(0.0)) + expprob*(saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJE[inxE(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] < (valueUnE[inx(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k)])))?(1.0):(0.0));

                    // and residual who star entrepreneur
                    expprob = mprod[tgrid][k]*mzprob[zgrid][e]*(MUENDOvecEhat[inxEE(igrid,tgrid,zgrid,e)])*(1.0 - phipar)*(1.0 - piW( searchJEi_W[inxEE(igrid,tgrid,zgrid, e)]));
                    distJE[inxE(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] += expprob*(1.0 - saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJE[inxE(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] >= (valueUnE[inx(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k)])))?(1.0):(0.0));
                    distJE[inxE(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] += expprob*(saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJE[inxE(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] >= (valueUnE[inx(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k)])))?(1.0):(0.0));
                        
                    // TRANSITION ENT TO UNINSURED UNEMPLOYED NOT-EXCLUDED (not receive job offer, receive phipar)
                    expprob = mprod[tgrid][k]*mzprob[zgrid][e]*(MUENDOvecEhat[inxEE(igrid,tgrid,zgrid,e)])*(phipar)*(1.0 - piW( searchJEi_W[inxEE(igrid,tgrid,zgrid, e)]));
                    distUnNE[inx(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k)] += expprob*(1.0 - saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] < (valueUnNE[inx(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k)])))?(1.0):(0.0));
                    distUnNE[inx(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k)] += expprob*(saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] < (valueUnNE[inx(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k)])))?(1.0):(0.0));
                    distJEtoUnNE[inxE(igrid, tgrid, zgrid)] += expprob*(1.0 - saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] < (valueUnNE[inx(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k)])))?(1.0):(0.0)) + expprob*(saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] < (valueUnNE[inx(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k)])))?(1.0):(0.0));

                    // and residual who star entrepreneur
                    expprob = mprod[tgrid][k]*mzprob[zgrid][e]*(MUENDOvecEhat[inxEE(igrid,tgrid,zgrid,e)])*(phipar)*(1.0 - piW( searchJEi_W[inxEE(igrid,tgrid,zgrid, e)]));
                    distJNE[inxE(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] += expprob*(1.0 - saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k, e)] >= (valueUnNE[inx(isaveJEi[inxEE(igrid, tgrid, zgrid, e)], k)])))?(1.0):(0.0));
                    distJNE[inxE(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] += expprob*(saveresJEi[inxEE(igrid, tgrid, zgrid, e)])*distJEiold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] >= (valueUnNE[inx(min((maxgrid-1),(isaveJEi[inxEE(igrid, tgrid, zgrid, e)]+1)), k)])))?(1.0):(0.0));

                #endif

                
                /*********************************/
                /*** ENTREPRENEUR NOT-INSURED NOT-EXCLUDED ***/
                /*********************************/
            
                // if the entrepreneur default
                if(defaultJNE[inxEE(igrid, tgrid, zgrid, e)] == 1.0) {
                    
                    // TRANSITION ENT TO WORKER EXCLUDED (receive job offer)
                    for(y=0; y < maxyprod; y++) {
                    
                        expprob = invmyprod[y]*mprod[tgrid][k]*mzprob[zgrid][e]*(piW( searchJNE_W[inxEE(igrid,tgrid,zgrid,e)]));
                        distWWE[inxW(isaveJNE[inxEE(igrid, tgrid, zgrid, e)], k, y)] += expprob*(1.0 - saveresJNE[inxEE(igrid, tgrid, zgrid, e)])*distJNEold[inxE(igrid, tgrid, zgrid)];
                        distWWE[inxW(min((maxgrid-1),(isaveJNE[inxEE(igrid, tgrid, zgrid, e)]+1)), k, y)] += expprob*(saveresJNE[inxEE(igrid, tgrid, zgrid, e)])*distJNEold[inxE(igrid, tgrid, zgrid)];
                        distJNEtoWWE[inxE(igrid, tgrid, zgrid)] += expprob*(1.0 - saveresJNE[inxEE(igrid, tgrid, zgrid, e)])*distJNEold[inxE(igrid, tgrid, zgrid)] + expprob*(saveresJNE[inxEE(igrid, tgrid, zgrid, e)])*distJNEold[inxE(igrid, tgrid, zgrid)];
                    }
                    
                    
                    // TRANSITION ENT TO UNEMPLOYED LT EXCLUDED
                    expprob = mprod[tgrid][k]*mzprob[zgrid][e]*(1.0 - piW( searchJNE_W[inxEE(igrid,tgrid,zgrid, e)]));
                    distUnE[inx(isaveJNE[inxEE(igrid, tgrid, zgrid, e)], k)] += expprob*(1.0 - saveresJNE[inxEE(igrid, tgrid, zgrid, e)])*distJNEold[inxE(igrid, tgrid, zgrid)];
                    distUnE[inx(min((maxgrid-1),(isaveJNE[inxEE(igrid, tgrid, zgrid, e)]+1)), k)] += expprob*(saveresJNE[inxEE(igrid, tgrid, zgrid, e)])*distJNEold[inxE(igrid, tgrid, zgrid)];
                    distJNEtoUnE[inxE(igrid, tgrid, zgrid)] += expprob*(1.0 - saveresJNE[inxEE(igrid, tgrid, zgrid, e)])*distJNEold[inxE(igrid, tgrid, zgrid)] + expprob*(saveresJNE[inxEE(igrid, tgrid, zgrid, e)])*distJNEold[inxE(igrid, tgrid, zgrid)];

                }
                
                
                
                // if the entrepreneur repay
                if(defaultJNE[inxEE(igrid, tgrid, zgrid, e)] == 0) {
                    
                    // TRANSITION ENT TO WORKER NOT EXCLUDED (receive job offer)
                    for(y=0; y < maxyprod; y++) {
                        
                        expprob = invmyprod[y]*mprod[tgrid][k]*mzprob[zgrid][e]*(piW( searchJNE_W[inxEE(igrid,tgrid,zgrid, e)]));
                        distWWNE[inxW(isaveJNE[inxEE(igrid, tgrid, zgrid, e)], k, y)] += expprob*(1.0 - saveresJNE[inxEE(igrid, tgrid, zgrid, e)])*distJNEold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(isaveJNE[inxEE(igrid, tgrid, zgrid, e)], k, e)] <= (valueWWNE[inxW(isaveJNE[inxEE(igrid, tgrid, zgrid, e)], k, y)])))?(1.0):(0.0));
                        distWWNE[inxW(min((maxgrid-1),(isaveJNE[inxEE(igrid, tgrid, zgrid, e)]+1)), k, y)] += expprob*(saveresJNE[inxEE(igrid, tgrid, zgrid, e)])*distJNEold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(min((maxgrid-1),(isaveJNE[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] <= (valueWWNE[inxW(min((maxgrid-1),(isaveJNE[inxEE(igrid, tgrid, zgrid, e)]+1)), k, y)])))?(1.0):(0.0));
                        distJNEtoWWNE[inxE(igrid, tgrid, zgrid)] += expprob*(1.0 - saveresJNE[inxEE(igrid, tgrid, zgrid, e)])*distJNEold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(isaveJNE[inxEE(igrid, tgrid, zgrid, e)], k, e)] <= (valueWWNE[inxW(isaveJNE[inxEE(igrid, tgrid, zgrid, e)], k, y)])))?(1.0):(0.0)) + expprob*(saveresJNE[inxEE(igrid, tgrid, zgrid, e)])*distJNEold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(min((maxgrid-1),(isaveJNE[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] <= (valueWWNE[inxW(min((maxgrid-1),(isaveJNE[inxEE(igrid, tgrid, zgrid, e)]+1)), k, y)])))?(1.0):(0.0));

                        
                        // residual who stay entrepreneur
                        expprob = invmyprod[y]*mprod[tgrid][k]*mzprob[zgrid][e]*(piW( searchJNE_W[inxEE(igrid,tgrid,zgrid, e)]));
                        distJNE[inxE(isaveJNE[inxEE(igrid, tgrid, zgrid, e)], k, e)] += expprob*(1.0 - saveresJNE[inxEE(igrid, tgrid, zgrid, e)])*distJNEold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(isaveJNE[inxEE(igrid, tgrid, zgrid, e)], k, e)] > (valueWWNE[inxW(isaveJNE[inxEE(igrid, tgrid, zgrid, e)], k, y)])))?(1.0):(0.0));
                        distJNE[inxE(min((maxgrid-1),(isaveJNE[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] += expprob*(saveresJNE[inxEE(igrid, tgrid, zgrid, e)])*distJNEold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(min((maxgrid-1),(isaveJNE[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] > (valueWWNE[inxW(min((maxgrid-1),(isaveJNE[inxEE(igrid, tgrid, zgrid, e)]+1)), k, y)])))?(1.0):(0.0));
                    }
                    

                    // TRANSITION ENT TO UNINSURED UNEMPLOYED NOT-EXCLUDED
                    expprob = mprod[tgrid][k]*mzprob[zgrid][e]*(1.0 - piW( searchJNE_W[inxEE(igrid,tgrid,zgrid, e)]));
                    distUnNE[inx(isaveJNE[inxEE(igrid, tgrid, zgrid, e)], k)] += expprob*(1.0 - saveresJNE[inxEE(igrid, tgrid, zgrid, e)])*distJNEold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(isaveJNE[inxEE(igrid, tgrid, zgrid, e)], k, e)] < (valueUnNE[inx(isaveJNE[inxEE(igrid, tgrid, zgrid, e)], k)])))?(1.0):(0.0));
                    distUnNE[inx(min((maxgrid-1),(isaveJNE[inxEE(igrid, tgrid, zgrid, e)]+1)), k)] += expprob*(saveresJNE[inxEE(igrid, tgrid, zgrid, e)])*distJNEold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(min((maxgrid-1),(isaveJNE[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] < (valueUnNE[inx(min((maxgrid-1),(isaveJNE[inxEE(igrid, tgrid, zgrid, e)]+1)), k)])))?(1.0):(0.0));
                    distJNEtoUnNE[inxE(igrid, tgrid, zgrid)] += expprob*(1.0 - saveresJNE[inxEE(igrid, tgrid, zgrid, e)])*distJNEold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(isaveJNE[inxEE(igrid, tgrid, zgrid, e)], k, e)] < (valueUnNE[inx(isaveJNE[inxEE(igrid, tgrid, zgrid, e)], k)])))?(1.0):(0.0)) + expprob*(saveresJNE[inxEE(igrid, tgrid, zgrid, e)])*distJNEold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(min((maxgrid-1),(isaveJNE[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] < (valueUnNE[inx(min((maxgrid-1),(isaveJNE[inxEE(igrid, tgrid, zgrid, e)]+1)), k)])))?(1.0):(0.0));
                    
                    // residual who stay entrepreneur
                    expprob = mprod[tgrid][k]*mzprob[zgrid][e]*(1.0 - piW( searchJNE_W[inxEE(igrid,tgrid,zgrid, e)]));
                    distJNE[inxE(isaveJNE[inxEE(igrid, tgrid, zgrid, e)], k, e)] += expprob*(1.0 - saveresJNE[inxEE(igrid, tgrid, zgrid, e)])*distJNEold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(isaveJNE[inxEE(igrid, tgrid, zgrid, e)], k, e)] >= (valueUnNE[inx(isaveJNE[inxEE(igrid, tgrid, zgrid, e)], k)])))?(1.0):(0.0));
                    distJNE[inxE(min((maxgrid-1),(isaveJNE[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] += expprob*(saveresJNE[inxEE(igrid, tgrid, zgrid, e)])*distJNEold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(min((maxgrid-1),(isaveJNE[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] >= (valueUnNE[inx(min((maxgrid-1),(isaveJNE[inxEE(igrid, tgrid, zgrid, e)]+1)), k)])))?(1.0):(0.0));
                } // end condition
                
                
                
                
                /*****************************/
                /*** ENTREPRENEUR EXCLUDED ***/
                /*****************************/
                
                for(y=0; y < maxyprod; y++) {
                
                    // TRANSITION ENT TO WORKER EXCLUDED (receive job offer, not receive phipar)
                    expprob = invmyprod[y]*mprod[tgrid][k]*mzprob[zgrid][e]*(1.0 - phipar)*(piW( searchJE_W[inxEE(igrid,tgrid,zgrid, e)]));
                    distWWE[inxW(isaveJE[inxEE(igrid, tgrid, zgrid, e)], k, y)] += expprob*(1.0 - saveresJE[inxEE(igrid, tgrid, zgrid, e)])*distJEold[inxE(igrid, tgrid, zgrid)]*(((valueJE[inxE(isaveJE[inxEE(igrid, tgrid, zgrid, e)], k, e)] <= (valueWWE[inxW(isaveJE[inxEE(igrid, tgrid, zgrid, e)], k, y)])))?(1.0):(0.0));
                    distWWE[inxW(min((maxgrid-1),(isaveJE[inxEE(igrid, tgrid, zgrid, e)]+1)), k, y)] += expprob*(saveresJE[inxEE(igrid, tgrid, zgrid, e)])*distJEold[inxE(igrid, tgrid, zgrid)]*(((valueJE[inxE(min((maxgrid-1),(isaveJE[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] <= (valueWWE[inxW(min((maxgrid-1),(isaveJE[inxEE(igrid, tgrid, zgrid, e)]+1)), k, y)])))?(1.0):(0.0));
                    distJEtoWWE[inxE(igrid, tgrid, zgrid)] += expprob*(1.0 - saveresJE[inxEE(igrid, tgrid, zgrid, e)])*distJEold[inxE(igrid, tgrid, zgrid)]*(((valueJE[inxE(isaveJE[inxEE(igrid, tgrid, zgrid, e)], k, e)] <= (valueWWE[inxW(isaveJE[inxEE(igrid, tgrid, zgrid, e)], k, y)])))?(1.0):(0.0)) + expprob*(saveresJE[inxEE(igrid, tgrid, zgrid, e)])*distJEold[inxE(igrid, tgrid, zgrid)]*(((valueJE[inxE(min((maxgrid-1),(isaveJE[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] <= (valueWWE[inxW(min((maxgrid-1),(isaveJE[inxEE(igrid, tgrid, zgrid, e)]+1)), k, y)])))?(1.0):(0.0));

                    // and residual who stay entrepreneur
                    expprob = invmyprod[y]*mprod[tgrid][k]*mzprob[zgrid][e]*(1.0 - phipar)*(piW( searchJE_W[inxEE(igrid,tgrid,zgrid, e)]));
                    distJE[inxE(isaveJE[inxEE(igrid, tgrid, zgrid, e)], k, e)] += expprob*(1.0 - saveresJE[inxEE(igrid, tgrid, zgrid, e)])*distJEold[inxE(igrid, tgrid, zgrid)]*(((valueJE[inxE(isaveJE[inxEE(igrid, tgrid, zgrid, e)], k, e)] > (valueWWE[inxW(isaveJE[inxEE(igrid, tgrid, zgrid, e)], k, y)])))?(1.0):(0.0));
                    distJE[inxE(min((maxgrid-1),(isaveJE[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] += expprob*(saveresJE[inxEE(igrid, tgrid, zgrid, e)])*distJEold[inxE(igrid, tgrid, zgrid)]*(((valueJE[inxE(min((maxgrid-1),(isaveJE[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] > (valueWWE[inxW(min((maxgrid-1),(isaveJE[inxEE(igrid, tgrid, zgrid, e)]+1)), k, y)])))?(1.0):(0.0));
                    
                    
                    // TRANSITION ENT TO WORKER NOT-EXCLUDED (receive job offer, receive phipar)
                    expprob = invmyprod[y]*mprod[tgrid][k]*mzprob[zgrid][e]*(phipar)*(piW( searchJE_W[inxEE(igrid,tgrid,zgrid, e)]));
                    distWWNE[inxW(isaveJE[inxEE(igrid, tgrid, zgrid, e)], k, y)] += expprob*(1.0 - saveresJE[inxEE(igrid, tgrid, zgrid, e)])*distJEold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(isaveJE[inxEE(igrid, tgrid, zgrid, e)], k, e)] <= (valueWWNE[inxW(isaveJE[inxEE(igrid, tgrid, zgrid, e)], k, y)])))?(1.0):(0.0));
                    distWWNE[inxW(min((maxgrid-1),(isaveJE[inxEE(igrid, tgrid, zgrid, e)]+1)), k, y)] += expprob*(saveresJE[inxEE(igrid, tgrid, zgrid, e)])*distJEold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(min((maxgrid-1),(isaveJE[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] <= (valueWWNE[inxW(min((maxgrid-1),(isaveJE[inxEE(igrid, tgrid, zgrid, e)]+1)), k, y)])))?(1.0):(0.0));
                    distJEtoWWNE[inxE(igrid, tgrid, zgrid)] += expprob*(1.0 - saveresJE[inxEE(igrid, tgrid, zgrid, e)])*distJEold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(isaveJE[inxEE(igrid, tgrid, zgrid, e)], k, e)] <= (valueWWNE[inxW(isaveJE[inxEE(igrid, tgrid, zgrid, e)], k, y)])))?(1.0):(0.0)) + expprob*(saveresJE[inxEE(igrid, tgrid, zgrid, e)])*distJEold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(min((maxgrid-1),(isaveJE[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] <= (valueWWNE[inxW(min((maxgrid-1),(isaveJE[inxEE(igrid, tgrid, zgrid, e)]+1)), k, y)])))?(1.0):(0.0));

                    // and residual who stay entrepreneur
                    expprob = invmyprod[y]*mprod[tgrid][k]*mzprob[zgrid][e]*(phipar)*(piW( searchJE_W[inxEE(igrid,tgrid,zgrid, e)]));
                    distJNE[inxE(isaveJE[inxEE(igrid, tgrid, zgrid, e)], k, e)] += expprob*(1.0 - saveresJE[inxEE(igrid, tgrid, zgrid, e)])*distJEold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(isaveJE[inxEE(igrid, tgrid, zgrid, e)], k, e)] > (valueWWNE[inxW(isaveJE[inxEE(igrid, tgrid, zgrid, e)], k, y)])))?(1.0):(0.0));
                    distJNE[inxE(min((maxgrid-1),(isaveJE[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] += expprob*(saveresJE[inxEE(igrid, tgrid, zgrid, e)])*distJEold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(min((maxgrid-1),(isaveJE[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] > (valueWWNE[inxW(min((maxgrid-1),(isaveJE[inxEE(igrid, tgrid, zgrid, e)]+1)), k, y)])))?(1.0):(0.0));
                }
                
                
                // TRANSITION ENT TO UNEMPLOYED LT EXCLUDED
                // TRANSITION ENT TO UNINSURED UNEMPLOYED EXCLUDED (receive no job offer, not receive phipar)
                expprob = mprod[tgrid][k]*mzprob[zgrid][e]*(1.0 - phipar)*(1.0 - piW( searchJE_W[inxEE(igrid,tgrid,zgrid, e)]));
                distUnE[inx(isaveJE[inxEE(igrid, tgrid, zgrid, e)], k)] += expprob*(1.0 - saveresJE[inxEE(igrid, tgrid, zgrid, e)])*distJEold[inxE(igrid, tgrid, zgrid)]*(((valueJE[inxE(isaveJE[inxEE(igrid, tgrid, zgrid, e)], k, e)] < (valueUnE[inx(isaveJE[inxEE(igrid, tgrid, zgrid, e)], k)])))?(1.0):(0.0));
                distUnE[inx(min((maxgrid-1),(isaveJE[inxEE(igrid, tgrid, zgrid, e)]+1)), k)] += expprob*(saveresJE[inxEE(igrid, tgrid, zgrid, e)])*distJEold[inxE(igrid, tgrid, zgrid)]*(((valueJE[inxE(min((maxgrid-1),(isaveJE[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] < (valueUnE[inx(min((maxgrid-1),(isaveJE[inxEE(igrid, tgrid, zgrid, e)]+1)), k)])))?(1.0):(0.0));
                distJEtoUnE[inxE(igrid, tgrid, zgrid)] += expprob*(1.0 - saveresJE[inxEE(igrid, tgrid, zgrid, e)])*distJEold[inxE(igrid, tgrid, zgrid)]*(((valueJE[inxE(isaveJE[inxEE(igrid, tgrid, zgrid, e)], k, e)] < (valueUnE[inx(isaveJE[inxEE(igrid, tgrid, zgrid, e)], k)])))?(1.0):(0.0)) + expprob*(saveresJE[inxEE(igrid, tgrid, zgrid, e)])*distJEold[inxE(igrid, tgrid, zgrid)]*(((valueJE[inxE(min((maxgrid-1),(isaveJE[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] < (valueUnE[inx(min((maxgrid-1),(isaveJE[inxEE(igrid, tgrid, zgrid, e)]+1)), k)])))?(1.0):(0.0));
                
                // and residual who star entrepreneur
                expprob = mprod[tgrid][k]*mzprob[zgrid][e]*(1.0 - phipar)*(1.0 - piW( searchJE_W[inxEE(igrid,tgrid,zgrid, e)]));
                distJE[inxE(isaveJE[inxEE(igrid, tgrid, zgrid, e)], k, e)] += expprob*(1.0 - saveresJE[inxEE(igrid, tgrid, zgrid, e)])*distJEold[inxE(igrid, tgrid, zgrid)]*(((valueJE[inxE(isaveJE[inxEE(igrid, tgrid, zgrid, e)], k, e)] >= (valueUnE[inx(isaveJE[inxEE(igrid, tgrid, zgrid, e)], k)])))?(1.0):(0.0));
                distJE[inxE(min((maxgrid-1),(isaveJE[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] += expprob*(saveresJE[inxEE(igrid, tgrid, zgrid, e)])*distJEold[inxE(igrid, tgrid, zgrid)]*(((valueJE[inxE(min((maxgrid-1),(isaveJE[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] >= (valueUnE[inx(min((maxgrid-1),(isaveJE[inxEE(igrid, tgrid, zgrid, e)]+1)), k)])))?(1.0):(0.0));
                
                // TRANSITION ENT TO UNINSURED UNEMPLOYED NOT-EXCLUDED (not receive job offer, receive phipar)
                expprob = mprod[tgrid][k]*mzprob[zgrid][e]*(phipar)*(1.0 - piW( searchJE_W[inxEE(igrid,tgrid,zgrid, e)]));
                distUnNE[inx(isaveJE[inxEE(igrid, tgrid, zgrid, e)], k)] += expprob*(1.0 - saveresJE[inxEE(igrid, tgrid, zgrid, e)])*distJEold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(isaveJE[inxEE(igrid, tgrid, zgrid, e)], k, e)] < (valueUnNE[inx(isaveJE[inxEE(igrid, tgrid, zgrid, e)], k)])))?(1.0):(0.0));
                distUnNE[inx(min((maxgrid-1),(isaveJE[inxEE(igrid, tgrid, zgrid, e)]+1)), k)] += expprob*(saveresJE[inxEE(igrid, tgrid, zgrid, e)])*distJEold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(min((maxgrid-1),(isaveJE[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] < (valueUnNE[inx(min((maxgrid-1),(isaveJE[inxEE(igrid, tgrid, zgrid, e)]+1)), k)])))?(1.0):(0.0));
                distJEtoUnNE[inxE(igrid, tgrid, zgrid)] += expprob*(1.0 - saveresJE[inxEE(igrid, tgrid, zgrid, e)])*distJEold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(isaveJE[inxEE(igrid, tgrid, zgrid, e)], k, e)] < (valueUnNE[inx(isaveJE[inxEE(igrid, tgrid, zgrid, e)], k)])))?(1.0):(0.0)) + expprob*(saveresJE[inxEE(igrid, tgrid, zgrid, e)])*distJEold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(min((maxgrid-1),(isaveJE[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] < (valueUnNE[inx(min((maxgrid-1),(isaveJE[inxEE(igrid, tgrid, zgrid, e)]+1)), k)])))?(1.0):(0.0));
                
                // and residual who star entrepreneur
                expprob = mprod[tgrid][k]*mzprob[zgrid][e]*(phipar)*(1.0 - piW( searchJE_W[inxEE(igrid,tgrid,zgrid, e)]));
                distJNE[inxE(isaveJE[inxEE(igrid, tgrid, zgrid, e)], k, e)] += expprob*(1.0 - saveresJE[inxEE(igrid, tgrid, zgrid, e)])*distJEold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(isaveJE[inxEE(igrid, tgrid, zgrid, e)], k, e)] >= (valueUnNE[inx(isaveJE[inxEE(igrid, tgrid, zgrid, e)], k)])))?(1.0):(0.0));
                distJNE[inxE(min((maxgrid-1),(isaveJE[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] += expprob*(saveresJE[inxEE(igrid, tgrid, zgrid, e)])*distJEold[inxE(igrid, tgrid, zgrid)]*(((valueJNE[inxE(min((maxgrid-1),(isaveJE[inxEE(igrid, tgrid, zgrid, e)]+1)), k, e)] >= (valueUnNE[inx(min((maxgrid-1),(isaveJE[inxEE(igrid, tgrid, zgrid, e)]+1)), k)])))?(1.0):(0.0));

                
            } // end e
            
        } // end zgrid
        
        } // end k prod
    } // end igrid
} // end tgrid computation of ergodic transition tgrid


    
// YOUNG ENTERING, OLD LEAVING //
for(tgrid = 0; tgrid<maxtheta; tgrid++) {
    for(igrid = 0; igrid<maxgrid; igrid++) {

        distUnNE[inx(igrid, tgrid)] += newyoung*(distUiE[inx(igrid, tgrid)] + distUiNE[inx(igrid, tgrid)] + distUnE[inx(igrid, tgrid)]);
        distUiE[inx(igrid, tgrid)]   = (1.0 - newyoung)*(distUiE[inx(igrid, tgrid)]);
        distUiNE[inx(igrid, tgrid)]  = (1.0 - newyoung)*(distUiNE[inx(igrid, tgrid)]);
        distUnE[inx(igrid, tgrid)]   = (1.0 - newyoung)*(distUnE[inx(igrid, tgrid)]);

        for(zgrid = 0; zgrid<maxfirmtype; zgrid++) {
            distUnNE[inx(igrid, tgrid)]       += newyoung*(distJE[inxE(igrid, tgrid, zgrid)]+distJNE[inxE(igrid, tgrid, zgrid)]);
            distJE[inxE(igrid, tgrid, zgrid)]  = (1.0 - newyoung)*(distJE[inxE(igrid, tgrid, zgrid)]);
            distJNE[inxE(igrid, tgrid, zgrid)] = (1.0 - newyoung)*(distJNE[inxE(igrid, tgrid, zgrid)]);
        }
        
        for(ygrid = 0; ygrid<maxyprod; ygrid++) {
            distUnNE[inx(igrid, tgrid)]        += newyoung*(distWWE[inxW(igrid, tgrid, ygrid)]+distWWNE[inxW(igrid, tgrid, ygrid)]);
            distWWE[inxW(igrid, tgrid, ygrid)]  = (1.0 - newyoung)*(distWWE[inxW(igrid, tgrid, ygrid)]);
            distWWNE[inxW(igrid, tgrid, ygrid)] = (1.0 - newyoung)*(distWWNE[inxW(igrid, tgrid, ygrid)]);
        }
    }
}



critDist = 0.0; distval  = 0.0;
for(tgrid = 0; tgrid<maxtheta; tgrid++){
    for(igrid=0;igrid<maxgrid;igrid++){
        distval +=distUiE[inx(igrid, tgrid)];
        distval +=distUnE[inx(igrid, tgrid)];
        distval +=distUiNE[inx(igrid, tgrid)];
        distval +=distUnNE[inx(igrid, tgrid)];
        
        distvalold +=distUiEold[inx(igrid, tgrid)];
        distvalold +=distUnEold[inx(igrid, tgrid)];
        distvalold +=distUiNEold[inx(igrid, tgrid)];
        distvalold +=distUnNEold[inx(igrid, tgrid)];
        
        critDist =(max(fabs(distUiE[inx(igrid, tgrid)]-distUiEold[inx(igrid, tgrid)]), critDist));
        critDist =(max(fabs(distUnE[inx(igrid, tgrid)]-distUnEold[inx(igrid, tgrid)]), critDist));
        critDist =(max(fabs(distUiNE[inx(igrid, tgrid)]-distUiNEold[inx(igrid, tgrid)]), critDist));
        critDist =(max(fabs(distUnNE[inx(igrid, tgrid)]-distUnNEold[inx(igrid, tgrid)]), critDist));
        
        for(ygrid=0; ygrid<maxyprod; ygrid++) {
            distval   +=distWWNE[inxW(igrid, tgrid, ygrid)];
            distval   +=distWWE[inxW(igrid, tgrid, ygrid)];
            distvalold+=distWWEold[inxW(igrid, tgrid, ygrid)];
            distvalold+=distWWNEold[inxW(igrid, tgrid, ygrid)];
            critDist   =(max(fabs(distWWNE[inxW(igrid, tgrid, ygrid)]-distWWNEold[inxW(igrid, tgrid, ygrid)]), critDist));
            critDist   =(max(fabs(distWWE[inxW(igrid, tgrid, ygrid)]-distWWEold[inxW(igrid, tgrid, ygrid)]), critDist));
        }
        
        for(zgrid=0; zgrid<maxfirmtype; zgrid++) {
            distval   +=distJE[inxE(igrid, tgrid, zgrid)];
            distval   +=distJNE[inxE(igrid, tgrid, zgrid)];
            distvalold+=distJNEold[inxE(igrid, tgrid, zgrid)];
            distvalold+=distJEold[inxE(igrid, tgrid, zgrid)];
            critDist   =(max(fabs(distJE[inxE(igrid, tgrid, zgrid)]-distJEold[inxE(igrid, tgrid, zgrid)]), critDist));
            critDist   =(max(fabs(distJNE[inxE(igrid, tgrid, zgrid)]-distJNEold[inxE(igrid, tgrid, zgrid)]), critDist));
        }
    }
}

//printf("Dist CNVG %d\t%20.15f\t%20.15f\n",itr,critDist,distval);

#if indexPM == 0
}//while((critDist>epsilonDist))
        
        
        
printf("Dist CNVG %d\t%20.15f\t%20.15f\n",itr,critDist,distval);


#endif
        


        
#include "compute_statistics.cpp"
        
        
        



// Clear memory
free(distUiEold);free(distUnEold);free(distWWEold);free(distUiNEold);free(distUnNEold);free(distWWNEold);free(distJEold);free(distJNEold);
free(saveresUiE);free(saveresUnE);free(saveresWWE);free(saveresUiNE);free(saveresUnNE);free(saveresWWNE);free(saveresJE);free(saveresJNE);
free(isaveJE);free(isaveJNE);free(isaveWWNE);free(isaveUiNE);free(isaveUnNE);free(isaveWWE);free(isaveUiE);free(isaveUnE);
#if SEA == 1
    free(distJEiold);free(distJNEiold);
    free(saveresJEi);free(saveresJNEi);free(isaveJEi);free(isaveJNEi);
    free(MUENDOvecB);free(MUENDOvecR);free(MUENDOvecEhat);
#endif


// clear statistics //
free(distUiEtoJE);free(distUiEtoJNE);free(distUiEtoWWE);free(distUiEtoWWNE);free(distUiEtoUnE);free(distUiEtoUnNE);free(distUiEtoUiNE);free(distUnEtoJE);free(distUnEtoJNE);free(distUnEtoWWE);free(distUnEtoWWNE);free(distUnEtoUnNE);free(distUiNEtoJNE);free(distUiNEtoWWNE);free(distUiNEtoUnNE);free(distUnNEtoJNE);free(distUnNEtoWWNE);free(distUiNEtoJNE_select);free(distUiEtoJE_select);free(distUiEtoJNE_select);free(distWWEtoJE);free(distWWEtoJNE);free(distWWEtoUiE);free(distWWEtoUiNE);free(distWWNEtoUiNE);free(distWWNEtoJNE);free(distJEtoWWE);free(distJEtoUiE);free(distJEtoUnE);free(distJEtoUiNE);free(distJEtoUnNE);free(distJEtoWWNE);free(distJNEtoWWE);free(distJNEtoUiE);free(distJNEtoUnE);free(distJNEtoUiNE);free(distJNEtoUnNE);free(distJNEtoWWNE);free(distWWEfromUiEtoJE);free(distWWEfromUiEtoJNE);free(distWWNEfromUiNEtoJNE);
free(necessityWWE);free(necessityWWNE);free(necessityUiE);free(necessityUiNE);free(necessityUnE);free(necessityUnNE);
free(distassetJ);free(distassetW);free(distassetUi);free(distassetUn);free(distasset);free(disttotal);free(distWWearning);
free(aggWWtoJ);free(aggJtoWW);free(aggWWtoU);free(aggJtoU);free(aggUtoJ);free(aggUtoWW);
free(fracWWpability);free(fracJpability);free(fracUpability);

#if SIMUL10 == 1
    free(distnewJE_Ui);free(distnewJNE_Ui);
    free(distnewJE_tot);free(distnewJNE_tot);
    free(distnewJE_baseline);free(distnewJNE_baseline);
    free(distnewJE_selected);free(distnewJNE_selected);
#endif


}




